blob: 86ff5855c9f97caad59918da465a60742a2d54b8 [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 Moolenaar77073442016-02-13 23:23:53 +0100513static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100514#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_changenr(typval_T *argvars, typval_T *rettv);
516static void f_char2nr(typval_T *argvars, typval_T *rettv);
517static void f_cindent(typval_T *argvars, typval_T *rettv);
518static void f_clearmatches(typval_T *argvars, typval_T *rettv);
519static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000520#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_complete(typval_T *argvars, typval_T *rettv);
522static void f_complete_add(typval_T *argvars, typval_T *rettv);
523static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000524#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100525static void f_confirm(typval_T *argvars, typval_T *rettv);
526static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_cos(typval_T *argvars, typval_T *rettv);
529static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_count(typval_T *argvars, typval_T *rettv);
532static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
533static void f_cursor(typval_T *argsvars, typval_T *rettv);
534static void f_deepcopy(typval_T *argvars, typval_T *rettv);
535static void f_delete(typval_T *argvars, typval_T *rettv);
536static void f_did_filetype(typval_T *argvars, typval_T *rettv);
537static void f_diff_filler(typval_T *argvars, typval_T *rettv);
538static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100539static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_empty(typval_T *argvars, typval_T *rettv);
541static void f_escape(typval_T *argvars, typval_T *rettv);
542static void f_eval(typval_T *argvars, typval_T *rettv);
543static void f_eventhandler(typval_T *argvars, typval_T *rettv);
544static void f_executable(typval_T *argvars, typval_T *rettv);
545static void f_exepath(typval_T *argvars, typval_T *rettv);
546static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200547#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_expand(typval_T *argvars, typval_T *rettv);
551static void f_extend(typval_T *argvars, typval_T *rettv);
552static void f_feedkeys(typval_T *argvars, typval_T *rettv);
553static void f_filereadable(typval_T *argvars, typval_T *rettv);
554static void f_filewritable(typval_T *argvars, typval_T *rettv);
555static void f_filter(typval_T *argvars, typval_T *rettv);
556static void f_finddir(typval_T *argvars, typval_T *rettv);
557static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_float2nr(typval_T *argvars, typval_T *rettv);
560static void f_floor(typval_T *argvars, typval_T *rettv);
561static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_fnameescape(typval_T *argvars, typval_T *rettv);
564static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
565static void f_foldclosed(typval_T *argvars, typval_T *rettv);
566static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
567static void f_foldlevel(typval_T *argvars, typval_T *rettv);
568static void f_foldtext(typval_T *argvars, typval_T *rettv);
569static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
570static void f_foreground(typval_T *argvars, typval_T *rettv);
571static void f_function(typval_T *argvars, typval_T *rettv);
572static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
573static void f_get(typval_T *argvars, typval_T *rettv);
574static void f_getbufline(typval_T *argvars, typval_T *rettv);
575static void f_getbufvar(typval_T *argvars, typval_T *rettv);
576static void f_getchar(typval_T *argvars, typval_T *rettv);
577static void f_getcharmod(typval_T *argvars, typval_T *rettv);
578static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
579static void f_getcmdline(typval_T *argvars, typval_T *rettv);
580static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
581static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
582static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
583static void f_getcwd(typval_T *argvars, typval_T *rettv);
584static void f_getfontname(typval_T *argvars, typval_T *rettv);
585static void f_getfperm(typval_T *argvars, typval_T *rettv);
586static void f_getfsize(typval_T *argvars, typval_T *rettv);
587static void f_getftime(typval_T *argvars, typval_T *rettv);
588static void f_getftype(typval_T *argvars, typval_T *rettv);
589static void f_getline(typval_T *argvars, typval_T *rettv);
590static void f_getmatches(typval_T *argvars, typval_T *rettv);
591static void f_getpid(typval_T *argvars, typval_T *rettv);
592static void f_getcurpos(typval_T *argvars, typval_T *rettv);
593static void f_getpos(typval_T *argvars, typval_T *rettv);
594static void f_getqflist(typval_T *argvars, typval_T *rettv);
595static void f_getreg(typval_T *argvars, typval_T *rettv);
596static void f_getregtype(typval_T *argvars, typval_T *rettv);
597static void f_gettabvar(typval_T *argvars, typval_T *rettv);
598static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
599static void f_getwinposx(typval_T *argvars, typval_T *rettv);
600static void f_getwinposy(typval_T *argvars, typval_T *rettv);
601static void f_getwinvar(typval_T *argvars, typval_T *rettv);
602static void f_glob(typval_T *argvars, typval_T *rettv);
603static void f_globpath(typval_T *argvars, typval_T *rettv);
604static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
605static void f_has(typval_T *argvars, typval_T *rettv);
606static void f_has_key(typval_T *argvars, typval_T *rettv);
607static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
608static void f_hasmapto(typval_T *argvars, typval_T *rettv);
609static void f_histadd(typval_T *argvars, typval_T *rettv);
610static void f_histdel(typval_T *argvars, typval_T *rettv);
611static void f_histget(typval_T *argvars, typval_T *rettv);
612static void f_histnr(typval_T *argvars, typval_T *rettv);
613static void f_hlID(typval_T *argvars, typval_T *rettv);
614static void f_hlexists(typval_T *argvars, typval_T *rettv);
615static void f_hostname(typval_T *argvars, typval_T *rettv);
616static void f_iconv(typval_T *argvars, typval_T *rettv);
617static void f_indent(typval_T *argvars, typval_T *rettv);
618static void f_index(typval_T *argvars, typval_T *rettv);
619static void f_input(typval_T *argvars, typval_T *rettv);
620static void f_inputdialog(typval_T *argvars, typval_T *rettv);
621static void f_inputlist(typval_T *argvars, typval_T *rettv);
622static void f_inputrestore(typval_T *argvars, typval_T *rettv);
623static void f_inputsave(typval_T *argvars, typval_T *rettv);
624static void f_inputsecret(typval_T *argvars, typval_T *rettv);
625static void f_insert(typval_T *argvars, typval_T *rettv);
626static void f_invert(typval_T *argvars, typval_T *rettv);
627static void f_isdirectory(typval_T *argvars, typval_T *rettv);
628static void f_islocked(typval_T *argvars, typval_T *rettv);
629static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100630#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100631# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100632static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100633# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +0100634static void f_job_start(typval_T *argvars, typval_T *rettv);
635static void f_job_stop(typval_T *argvars, typval_T *rettv);
636static void f_job_status(typval_T *argvars, typval_T *rettv);
637#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100638static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100639static void f_js_decode(typval_T *argvars, typval_T *rettv);
640static void f_js_encode(typval_T *argvars, typval_T *rettv);
641static void f_json_decode(typval_T *argvars, typval_T *rettv);
642static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100643static void f_keys(typval_T *argvars, typval_T *rettv);
644static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
645static void f_len(typval_T *argvars, typval_T *rettv);
646static void f_libcall(typval_T *argvars, typval_T *rettv);
647static void f_libcallnr(typval_T *argvars, typval_T *rettv);
648static void f_line(typval_T *argvars, typval_T *rettv);
649static void f_line2byte(typval_T *argvars, typval_T *rettv);
650static void f_lispindent(typval_T *argvars, typval_T *rettv);
651static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000652#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100653static void f_log(typval_T *argvars, typval_T *rettv);
654static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200656#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200658#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_map(typval_T *argvars, typval_T *rettv);
660static void f_maparg(typval_T *argvars, typval_T *rettv);
661static void f_mapcheck(typval_T *argvars, typval_T *rettv);
662static void f_match(typval_T *argvars, typval_T *rettv);
663static void f_matchadd(typval_T *argvars, typval_T *rettv);
664static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
665static void f_matcharg(typval_T *argvars, typval_T *rettv);
666static void f_matchdelete(typval_T *argvars, typval_T *rettv);
667static void f_matchend(typval_T *argvars, typval_T *rettv);
668static void f_matchlist(typval_T *argvars, typval_T *rettv);
669static void f_matchstr(typval_T *argvars, typval_T *rettv);
670static void f_max(typval_T *argvars, typval_T *rettv);
671static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000672#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100673static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000674#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100676#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100678#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
680static void f_nr2char(typval_T *argvars, typval_T *rettv);
681static void f_or(typval_T *argvars, typval_T *rettv);
682static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100683#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100684static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100685#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000686#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
690static void f_printf(typval_T *argvars, typval_T *rettv);
691static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200692#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200694#endif
695#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200697#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_range(typval_T *argvars, typval_T *rettv);
699static void f_readfile(typval_T *argvars, typval_T *rettv);
700static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100701#ifdef FEAT_FLOAT
702static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
703#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_reltimestr(typval_T *argvars, typval_T *rettv);
705static void f_remote_expr(typval_T *argvars, typval_T *rettv);
706static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
707static void f_remote_peek(typval_T *argvars, typval_T *rettv);
708static void f_remote_read(typval_T *argvars, typval_T *rettv);
709static void f_remote_send(typval_T *argvars, typval_T *rettv);
710static void f_remove(typval_T *argvars, typval_T *rettv);
711static void f_rename(typval_T *argvars, typval_T *rettv);
712static void f_repeat(typval_T *argvars, typval_T *rettv);
713static void f_resolve(typval_T *argvars, typval_T *rettv);
714static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000715#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000717#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_screenattr(typval_T *argvars, typval_T *rettv);
719static void f_screenchar(typval_T *argvars, typval_T *rettv);
720static void f_screencol(typval_T *argvars, typval_T *rettv);
721static void f_screenrow(typval_T *argvars, typval_T *rettv);
722static void f_search(typval_T *argvars, typval_T *rettv);
723static void f_searchdecl(typval_T *argvars, typval_T *rettv);
724static void f_searchpair(typval_T *argvars, typval_T *rettv);
725static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
726static void f_searchpos(typval_T *argvars, typval_T *rettv);
727static void f_server2client(typval_T *argvars, typval_T *rettv);
728static void f_serverlist(typval_T *argvars, typval_T *rettv);
729static void f_setbufvar(typval_T *argvars, typval_T *rettv);
730static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
731static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
732static void f_setline(typval_T *argvars, typval_T *rettv);
733static void f_setloclist(typval_T *argvars, typval_T *rettv);
734static void f_setmatches(typval_T *argvars, typval_T *rettv);
735static void f_setpos(typval_T *argvars, typval_T *rettv);
736static void f_setqflist(typval_T *argvars, typval_T *rettv);
737static void f_setreg(typval_T *argvars, typval_T *rettv);
738static void f_settabvar(typval_T *argvars, typval_T *rettv);
739static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
740static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100741#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100742static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100743#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100744static void f_shellescape(typval_T *argvars, typval_T *rettv);
745static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
746static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000747#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_sin(typval_T *argvars, typval_T *rettv);
749static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_sort(typval_T *argvars, typval_T *rettv);
752static void f_soundfold(typval_T *argvars, typval_T *rettv);
753static void f_spellbadword(typval_T *argvars, typval_T *rettv);
754static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
755static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_sqrt(typval_T *argvars, typval_T *rettv);
758static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_str2nr(typval_T *argvars, typval_T *rettv);
761static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000762#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000764#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_stridx(typval_T *argvars, typval_T *rettv);
766static void f_string(typval_T *argvars, typval_T *rettv);
767static void f_strlen(typval_T *argvars, typval_T *rettv);
768static void f_strpart(typval_T *argvars, typval_T *rettv);
769static void f_strridx(typval_T *argvars, typval_T *rettv);
770static void f_strtrans(typval_T *argvars, typval_T *rettv);
771static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
772static void f_strwidth(typval_T *argvars, typval_T *rettv);
773static void f_submatch(typval_T *argvars, typval_T *rettv);
774static void f_substitute(typval_T *argvars, typval_T *rettv);
775static void f_synID(typval_T *argvars, typval_T *rettv);
776static void f_synIDattr(typval_T *argvars, typval_T *rettv);
777static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
778static void f_synstack(typval_T *argvars, typval_T *rettv);
779static void f_synconcealed(typval_T *argvars, typval_T *rettv);
780static void f_system(typval_T *argvars, typval_T *rettv);
781static void f_systemlist(typval_T *argvars, typval_T *rettv);
782static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
783static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
784static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
785static void f_taglist(typval_T *argvars, typval_T *rettv);
786static void f_tagfiles(typval_T *argvars, typval_T *rettv);
787static void f_tempname(typval_T *argvars, typval_T *rettv);
788static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200789#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100790static void f_tan(typval_T *argvars, typval_T *rettv);
791static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200792#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100793static void f_tolower(typval_T *argvars, typval_T *rettv);
794static void f_toupper(typval_T *argvars, typval_T *rettv);
795static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000796#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100797static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000798#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100799static void f_type(typval_T *argvars, typval_T *rettv);
800static void f_undofile(typval_T *argvars, typval_T *rettv);
801static void f_undotree(typval_T *argvars, typval_T *rettv);
802static void f_uniq(typval_T *argvars, typval_T *rettv);
803static void f_values(typval_T *argvars, typval_T *rettv);
804static void f_virtcol(typval_T *argvars, typval_T *rettv);
805static void f_visualmode(typval_T *argvars, typval_T *rettv);
806static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
807static void f_winbufnr(typval_T *argvars, typval_T *rettv);
808static void f_wincol(typval_T *argvars, typval_T *rettv);
809static void f_winheight(typval_T *argvars, typval_T *rettv);
810static void f_winline(typval_T *argvars, typval_T *rettv);
811static void f_winnr(typval_T *argvars, typval_T *rettv);
812static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
813static void f_winrestview(typval_T *argvars, typval_T *rettv);
814static void f_winsaveview(typval_T *argvars, typval_T *rettv);
815static void f_winwidth(typval_T *argvars, typval_T *rettv);
816static void f_writefile(typval_T *argvars, typval_T *rettv);
817static void f_wordcount(typval_T *argvars, typval_T *rettv);
818static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000819
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100820static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
821static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
822static int get_env_len(char_u **arg);
823static int get_id_len(char_u **arg);
824static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
825static 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 +0000826#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
827#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
828 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100829static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
830static int eval_isnamec(int c);
831static int eval_isnamec1(int c);
832static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
833static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100834static typval_T *alloc_string_tv(char_u *string);
835static void init_tv(typval_T *varp);
836static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100837#ifdef FEAT_FLOAT
838static float_T get_tv_float(typval_T *varp);
839#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100840static linenr_T get_tv_lnum(typval_T *argvars);
841static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
842static char_u *get_tv_string(typval_T *varp);
843static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
844static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
845static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
846static hashtab_T *find_var_ht(char_u *name, char_u **varname);
847static funccall_T *get_funccal(void);
848static void vars_clear_ext(hashtab_T *ht, int free_val);
849static void delete_var(hashtab_T *ht, hashitem_T *hi);
850static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
851static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
852static void set_var(char_u *name, typval_T *varp, int copy);
853static int var_check_ro(int flags, char_u *name, int use_gettext);
854static int var_check_fixed(int flags, char_u *name, int use_gettext);
855static int var_check_func_name(char_u *name, int new_var);
856static int valid_varname(char_u *varname);
857static int tv_check_lock(int lock, char_u *name, int use_gettext);
858static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
859static char_u *find_option_end(char_u **arg, int *opt_flags);
860static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
861static int eval_fname_script(char_u *p);
862static int eval_fname_sid(char_u *p);
863static void list_func_head(ufunc_T *fp, int indent);
864static ufunc_T *find_func(char_u *name);
865static int function_exists(char_u *name);
866static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000867#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100868static void func_do_profile(ufunc_T *fp);
869static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
870static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000871static int
872# ifdef __BORLANDC__
873 _RTLENTRYF
874# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100875 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000876static int
877# ifdef __BORLANDC__
878 _RTLENTRYF
879# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000881#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100882static int script_autoload(char_u *name, int reload);
883static char_u *autoload_name(char_u *name);
884static void cat_func_name(char_u *buf, ufunc_T *fp);
885static void func_free(ufunc_T *fp);
886static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
887static int can_free_funccal(funccall_T *fc, int copyID) ;
888static void free_funccal(funccall_T *fc, int free_val);
889static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
890static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
891static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
892static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
893static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
894static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
895static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
896static int write_list(FILE *fd, list_T *list, int binary);
897static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000898
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200899
900#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100901static int compare_func_name(const void *s1, const void *s2);
902static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200903#endif
904
Bram Moolenaar33570922005-01-25 22:26:29 +0000905/*
906 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000907 */
908 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100909eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000910{
Bram Moolenaar33570922005-01-25 22:26:29 +0000911 int i;
912 struct vimvar *p;
913
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200914 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
915 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200916 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000917 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000918 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000919
920 for (i = 0; i < VV_LEN; ++i)
921 {
922 p = &vimvars[i];
923 STRCPY(p->vv_di.di_key, p->vv_name);
924 if (p->vv_flags & VV_RO)
925 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
926 else if (p->vv_flags & VV_RO_SBX)
927 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
928 else
929 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000930
931 /* add to v: scope dict, unless the value is not always available */
932 if (p->vv_type != VAR_UNKNOWN)
933 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000934 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000935 /* add to compat scope dict */
936 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000937 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100938 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
939
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000940 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100941 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200942 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100943 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100944
945 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
946 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
947 set_vim_var_nr(VV_NONE, VVAL_NONE);
948 set_vim_var_nr(VV_NULL, VVAL_NULL);
949
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200950 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200951
952#ifdef EBCDIC
953 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100954 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200955 */
956 sortFunctions();
957#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000958}
959
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000960#if defined(EXITFREE) || defined(PROTO)
961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100962eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000963{
964 int i;
965 struct vimvar *p;
966
967 for (i = 0; i < VV_LEN; ++i)
968 {
969 p = &vimvars[i];
970 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000971 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000972 vim_free(p->vv_str);
973 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000974 }
975 else if (p->vv_di.di_tv.v_type == VAR_LIST)
976 {
977 list_unref(p->vv_list);
978 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000979 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000980 }
981 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000982 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000983 hash_clear(&compat_hashtab);
984
Bram Moolenaard9fba312005-06-26 22:34:35 +0000985 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100986# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200987 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100988# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989
990 /* global variables */
991 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000992
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000993 /* autoloaded script names */
994 ga_clear_strings(&ga_loaded);
995
Bram Moolenaarcca74132013-09-25 21:00:28 +0200996 /* Script-local variables. First clear all the variables and in a second
997 * loop free the scriptvar_T, because a variable in one script might hold
998 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200999 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001000 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001001 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001002 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001003 ga_clear(&ga_scripts);
1004
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001005 /* unreferenced lists and dicts */
1006 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001007
1008 /* functions */
1009 free_all_functions();
1010 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001011}
1012#endif
1013
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 * Return the name of the executed function.
1016 */
1017 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001018func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021}
1022
1023/*
1024 * Return the address holding the next breakpoint line for a funccall cookie.
1025 */
1026 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001027func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaar33570922005-01-25 22:26:29 +00001029 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
1031
1032/*
1033 * Return the address holding the debug tick for a funccall cookie.
1034 */
1035 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001036func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
Bram Moolenaar33570922005-01-25 22:26:29 +00001038 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039}
1040
1041/*
1042 * Return the nesting level for a funccall cookie.
1043 */
1044 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001045func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046{
Bram Moolenaar33570922005-01-25 22:26:29 +00001047 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048}
1049
1050/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001051funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001053/* pointer to list of previously used funccal, still around because some
1054 * item in it is still being used. */
1055funccall_T *previous_funccal = NULL;
1056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057/*
1058 * Return TRUE when a function was ended by a ":return" command.
1059 */
1060 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001061current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
1063 return current_funccal->returned;
1064}
1065
1066
1067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 * Set an internal variable to a string value. Creates the variable if it does
1069 * not already exist.
1070 */
1071 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001072set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001074 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001075 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
1077 val = vim_strsave(value);
1078 if (val != NULL)
1079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001080 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001081 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001083 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001084 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 }
1086 }
1087}
1088
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001090static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091static char_u *redir_endp = NULL;
1092static char_u *redir_varname = NULL;
1093
1094/*
1095 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001096 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 * Returns OK if successfully completed the setup. FAIL otherwise.
1098 */
1099 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001100var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101{
1102 int save_emsg;
1103 int err;
1104 typval_T tv;
1105
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001106 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001107 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 {
1109 EMSG(_(e_invarg));
1110 return FAIL;
1111 }
1112
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001113 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 redir_varname = vim_strsave(name);
1115 if (redir_varname == NULL)
1116 return FAIL;
1117
1118 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1119 if (redir_lval == NULL)
1120 {
1121 var_redir_stop();
1122 return FAIL;
1123 }
1124
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 /* The output is stored in growarray "redir_ga" until redirection ends. */
1126 ga_init2(&redir_ga, (int)sizeof(char), 500);
1127
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001129 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001130 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1132 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001133 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 if (redir_endp != NULL && *redir_endp != NUL)
1135 /* Trailing characters are present after the variable name */
1136 EMSG(_(e_trailing));
1137 else
1138 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 var_redir_stop();
1141 return FAIL;
1142 }
1143
1144 /* check if we can write to the variable: set it to or append an empty
1145 * string */
1146 save_emsg = did_emsg;
1147 did_emsg = FALSE;
1148 tv.v_type = VAR_STRING;
1149 tv.vval.v_string = (char_u *)"";
1150 if (append)
1151 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1152 else
1153 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001154 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001156 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 if (err)
1158 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 var_redir_stop();
1161 return FAIL;
1162 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163
1164 return OK;
1165}
1166
1167/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001168 * Append "value[value_len]" to the variable set by var_redir_start().
1169 * The actual appending is postponed until redirection ends, because the value
1170 * appended may in fact be the string we write to, changing it may cause freed
1171 * memory to be used:
1172 * :redir => foo
1173 * :let foo
1174 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175 */
1176 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001177var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001179 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180
1181 if (redir_lval == NULL)
1182 return;
1183
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001184 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001185 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001187 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001189 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001190 {
1191 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001192 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193 }
1194 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196}
1197
1198/*
1199 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001200 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 */
1202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001203var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001205 typval_T tv;
1206
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 if (redir_lval != NULL)
1208 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001209 /* If there was no error: assign the text to the variable. */
1210 if (redir_endp != NULL)
1211 {
1212 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1213 tv.v_type = VAR_STRING;
1214 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001215 /* Call get_lval() again, if it's inside a Dict or List it may
1216 * have changed. */
1217 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001218 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001219 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1220 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1221 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001222 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001223
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001224 /* free the collected output */
1225 vim_free(redir_ga.ga_data);
1226 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001227
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228 vim_free(redir_lval);
1229 redir_lval = NULL;
1230 }
1231 vim_free(redir_varname);
1232 redir_varname = NULL;
1233}
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235# if defined(FEAT_MBYTE) || defined(PROTO)
1236 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001237eval_charconvert(
1238 char_u *enc_from,
1239 char_u *enc_to,
1240 char_u *fname_from,
1241 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 int err = FALSE;
1244
1245 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1246 set_vim_var_string(VV_CC_TO, enc_to, -1);
1247 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1248 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1249 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1250 err = TRUE;
1251 set_vim_var_string(VV_CC_FROM, NULL, -1);
1252 set_vim_var_string(VV_CC_TO, NULL, -1);
1253 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1254 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1255
1256 if (err)
1257 return FAIL;
1258 return OK;
1259}
1260# endif
1261
1262# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001264eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265{
1266 int err = FALSE;
1267
1268 set_vim_var_string(VV_FNAME_IN, fname, -1);
1269 set_vim_var_string(VV_CMDARG, args, -1);
1270 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1271 err = TRUE;
1272 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1273 set_vim_var_string(VV_CMDARG, NULL, -1);
1274
1275 if (err)
1276 {
1277 mch_remove(fname);
1278 return FAIL;
1279 }
1280 return OK;
1281}
1282# endif
1283
1284# if defined(FEAT_DIFF) || defined(PROTO)
1285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001286eval_diff(
1287 char_u *origfile,
1288 char_u *newfile,
1289 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290{
1291 int err = FALSE;
1292
1293 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1294 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1295 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1296 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1297 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1298 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1299 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1300}
1301
1302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001303eval_patch(
1304 char_u *origfile,
1305 char_u *difffile,
1306 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 int err;
1309
1310 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1311 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1312 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1313 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1314 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1315 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1316 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1317}
1318# endif
1319
1320/*
1321 * Top level evaluation function, returning a boolean.
1322 * Sets "error" to TRUE if there was an error.
1323 * Return TRUE or FALSE.
1324 */
1325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001326eval_to_bool(
1327 char_u *arg,
1328 int *error,
1329 char_u **nextcmd,
1330 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
Bram Moolenaar33570922005-01-25 22:26:29 +00001332 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 int retval = FALSE;
1334
1335 if (skip)
1336 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 else
1340 {
1341 *error = FALSE;
1342 if (!skip)
1343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001344 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 }
1347 }
1348 if (skip)
1349 --emsg_skip;
1350
1351 return retval;
1352}
1353
1354/*
1355 * Top level evaluation function, returning a string. If "skip" is TRUE,
1356 * only parsing to "nextcmd" is done, without reporting errors. Return
1357 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1358 */
1359 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001360eval_to_string_skip(
1361 char_u *arg,
1362 char_u **nextcmd,
1363 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
Bram Moolenaar33570922005-01-25 22:26:29 +00001365 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 char_u *retval;
1367
1368 if (skip)
1369 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 retval = NULL;
1372 else
1373 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 retval = vim_strsave(get_tv_string(&tv));
1375 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377 if (skip)
1378 --emsg_skip;
1379
1380 return retval;
1381}
1382
1383/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001384 * Skip over an expression at "*pp".
1385 * Return FAIL for an error, OK otherwise.
1386 */
1387 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001388skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001389{
Bram Moolenaar33570922005-01-25 22:26:29 +00001390 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001391
1392 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001394}
1395
1396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001398 * When "convert" is TRUE convert a List into a sequence of lines and convert
1399 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 * Return pointer to allocated memory, or NULL for failure.
1401 */
1402 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001403eval_to_string(
1404 char_u *arg,
1405 char_u **nextcmd,
1406 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaar33570922005-01-25 22:26:29 +00001408 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001410 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001411#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001412 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 retval = NULL;
1417 else
1418 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001419 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001420 {
1421 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001422 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001423 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001424 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001425 if (tv.vval.v_list->lv_len > 0)
1426 ga_append(&ga, NL);
1427 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001428 ga_append(&ga, NUL);
1429 retval = (char_u *)ga.ga_data;
1430 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001431#ifdef FEAT_FLOAT
1432 else if (convert && tv.v_type == VAR_FLOAT)
1433 {
1434 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1435 retval = vim_strsave(numbuf);
1436 }
1437#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001438 else
1439 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001440 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442
1443 return retval;
1444}
1445
1446/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001447 * Call eval_to_string() without using current local variables and using
1448 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 */
1450 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001451eval_to_string_safe(
1452 char_u *arg,
1453 char_u **nextcmd,
1454 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
1456 char_u *retval;
1457 void *save_funccalp;
1458
1459 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001460 if (use_sandbox)
1461 ++sandbox;
1462 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001463 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001464 if (use_sandbox)
1465 --sandbox;
1466 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 restore_funccal(save_funccalp);
1468 return retval;
1469}
1470
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471/*
1472 * Top level evaluation function, returning a number.
1473 * Evaluates "expr" silently.
1474 * Returns -1 for an error.
1475 */
1476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001477eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
Bram Moolenaar33570922005-01-25 22:26:29 +00001479 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001481 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
1483 ++emsg_off;
1484
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 retval = -1;
1487 else
1488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001489 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001490 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492 --emsg_off;
1493
1494 return retval;
1495}
1496
Bram Moolenaara40058a2005-07-11 22:42:07 +00001497/*
1498 * Prepare v: variable "idx" to be used.
1499 * Save the current typeval in "save_tv".
1500 * When not used yet add the variable to the v: hashtable.
1501 */
1502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001503prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001504{
1505 *save_tv = vimvars[idx].vv_tv;
1506 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1507 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1508}
1509
1510/*
1511 * Restore v: variable "idx" to typeval "save_tv".
1512 * When no longer defined, remove the variable from the v: hashtable.
1513 */
1514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001515restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001516{
1517 hashitem_T *hi;
1518
Bram Moolenaara40058a2005-07-11 22:42:07 +00001519 vimvars[idx].vv_tv = *save_tv;
1520 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1521 {
1522 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1523 if (HASHITEM_EMPTY(hi))
1524 EMSG2(_(e_intern2), "restore_vimvar()");
1525 else
1526 hash_remove(&vimvarht, hi);
1527 }
1528}
1529
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001530#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001531/*
1532 * Evaluate an expression to a list with suggestions.
1533 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001534 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001535 */
1536 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001537eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001538{
1539 typval_T save_val;
1540 typval_T rettv;
1541 list_T *list = NULL;
1542 char_u *p = skipwhite(expr);
1543
1544 /* Set "v:val" to the bad word. */
1545 prepare_vimvar(VV_VAL, &save_val);
1546 vimvars[VV_VAL].vv_type = VAR_STRING;
1547 vimvars[VV_VAL].vv_str = badword;
1548 if (p_verbose == 0)
1549 ++emsg_off;
1550
1551 if (eval1(&p, &rettv, TRUE) == OK)
1552 {
1553 if (rettv.v_type != VAR_LIST)
1554 clear_tv(&rettv);
1555 else
1556 list = rettv.vval.v_list;
1557 }
1558
1559 if (p_verbose == 0)
1560 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001561 restore_vimvar(VV_VAL, &save_val);
1562
1563 return list;
1564}
1565
1566/*
1567 * "list" is supposed to contain two items: a word and a number. Return the
1568 * word in "pp" and the number as the return value.
1569 * Return -1 if anything isn't right.
1570 * Used to get the good word and score from the eval_spell_expr() result.
1571 */
1572 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001573get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001574{
1575 listitem_T *li;
1576
1577 li = list->lv_first;
1578 if (li == NULL)
1579 return -1;
1580 *pp = get_tv_string(&li->li_tv);
1581
1582 li = li->li_next;
1583 if (li == NULL)
1584 return -1;
1585 return get_tv_number(&li->li_tv);
1586}
1587#endif
1588
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001589/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001590 * Top level evaluation function.
1591 * Returns an allocated typval_T with the result.
1592 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001593 */
1594 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001595eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001596{
1597 typval_T *tv;
1598
1599 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001600 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001601 {
1602 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001603 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001604 }
1605
1606 return tv;
1607}
1608
1609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001612 * Uses argv[argc] for the function arguments. Only Number and String
1613 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001616 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001617call_vim_function(
1618 char_u *func,
1619 int argc,
1620 char_u **argv,
1621 int safe, /* use the sandbox */
1622 int str_arg_only, /* all arguments are strings */
1623 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
Bram Moolenaar33570922005-01-25 22:26:29 +00001625 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 long n;
1627 int len;
1628 int i;
1629 int doesrange;
1630 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001631 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001633 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 for (i = 0; i < argc; i++)
1638 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001639 /* Pass a NULL or empty argument as an empty string */
1640 if (argv[i] == NULL || *argv[i] == NUL)
1641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001642 argvars[i].v_type = VAR_STRING;
1643 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001644 continue;
1645 }
1646
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001647 if (str_arg_only)
1648 len = 0;
1649 else
1650 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001651 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (len != 0 && len == (int)STRLEN(argv[i]))
1653 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001654 argvars[i].v_type = VAR_NUMBER;
1655 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 }
1657 else
1658 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001659 argvars[i].v_type = VAR_STRING;
1660 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
1662 }
1663
1664 if (safe)
1665 {
1666 save_funccalp = save_funccal();
1667 ++sandbox;
1668 }
1669
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001670 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1671 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (safe)
1675 {
1676 --sandbox;
1677 restore_funccal(save_funccalp);
1678 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 vim_free(argvars);
1680
1681 if (ret == FAIL)
1682 clear_tv(rettv);
1683
1684 return ret;
1685}
1686
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001687/*
1688 * Call vimL function "func" and return the result as a number.
1689 * Returns -1 when calling the function fails.
1690 * Uses argv[argc] for the function arguments.
1691 */
1692 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001693call_func_retnr(
1694 char_u *func,
1695 int argc,
1696 char_u **argv,
1697 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001698{
1699 typval_T rettv;
1700 long retval;
1701
1702 /* All arguments are passed as strings, no conversion to number. */
1703 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1704 return -1;
1705
1706 retval = get_tv_number_chk(&rettv, NULL);
1707 clear_tv(&rettv);
1708 return retval;
1709}
1710
1711#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1712 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1713
Bram Moolenaar4f688582007-07-24 12:34:30 +00001714# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001716 * Call vimL function "func" and return the result as a string.
1717 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718 * Uses argv[argc] for the function arguments.
1719 */
1720 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001721call_func_retstr(
1722 char_u *func,
1723 int argc,
1724 char_u **argv,
1725 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001726{
1727 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001728 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001729
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001730 /* All arguments are passed as strings, no conversion to number. */
1731 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732 return NULL;
1733
1734 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 return retval;
1737}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001738# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001740/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001741 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001742 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001743 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 */
1745 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001746call_func_retlist(
1747 char_u *func,
1748 int argc,
1749 char_u **argv,
1750 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751{
1752 typval_T rettv;
1753
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001754 /* All arguments are passed as strings, no conversion to number. */
1755 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756 return NULL;
1757
1758 if (rettv.v_type != VAR_LIST)
1759 {
1760 clear_tv(&rettv);
1761 return NULL;
1762 }
1763
1764 return rettv.vval.v_list;
1765}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#endif
1767
1768/*
1769 * Save the current function call pointer, and set it to NULL.
1770 * Used when executing autocommands and for ":source".
1771 */
1772 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001773save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001775 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 current_funccal = NULL;
1778 return (void *)fc;
1779}
1780
1781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001782restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001784 funccall_T *fc = (funccall_T *)vfc;
1785
1786 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787}
1788
Bram Moolenaar05159a02005-02-26 23:04:13 +00001789#if defined(FEAT_PROFILE) || defined(PROTO)
1790/*
1791 * Prepare profiling for entering a child or something else that is not
1792 * counted for the script/function itself.
1793 * Should always be called in pair with prof_child_exit().
1794 */
1795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001796prof_child_enter(
1797 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001798{
1799 funccall_T *fc = current_funccal;
1800
1801 if (fc != NULL && fc->func->uf_profiling)
1802 profile_start(&fc->prof_child);
1803 script_prof_save(tm);
1804}
1805
1806/*
1807 * Take care of time spent in a child.
1808 * Should always be called after prof_child_enter().
1809 */
1810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001811prof_child_exit(
1812 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001813{
1814 funccall_T *fc = current_funccal;
1815
1816 if (fc != NULL && fc->func->uf_profiling)
1817 {
1818 profile_end(&fc->prof_child);
1819 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1820 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1821 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1822 }
1823 script_prof_restore(tm);
1824}
1825#endif
1826
1827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828#ifdef FEAT_FOLDING
1829/*
1830 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1831 * it in "*cp". Doesn't give error messages.
1832 */
1833 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001834eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
Bram Moolenaar33570922005-01-25 22:26:29 +00001836 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 int retval;
1838 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001839 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1840 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
1842 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001843 if (use_sandbox)
1844 ++sandbox;
1845 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 retval = 0;
1849 else
1850 {
1851 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 if (tv.v_type == VAR_NUMBER)
1853 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001854 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 retval = 0;
1856 else
1857 {
1858 /* If the result is a string, check if there is a non-digit before
1859 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (!VIM_ISDIGIT(*s) && *s != '-')
1862 *cp = *s++;
1863 retval = atol((char *)s);
1864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
1867 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001868 if (use_sandbox)
1869 --sandbox;
1870 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
1872 return retval;
1873}
1874#endif
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 * ":let" list all variable values
1878 * ":let var1 var2" list variable values
1879 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001880 * ":let var += expr" assignment command.
1881 * ":let var -= expr" assignment command.
1882 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 */
1885 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001886ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887{
1888 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001890 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 int var_count = 0;
1893 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001895 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001896 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
Bram Moolenaardb552d602006-03-23 22:59:57 +00001898 argend = skip_var_list(arg, &var_count, &semicolon);
1899 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001901 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1902 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001903 expr = skipwhite(argend);
1904 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1905 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001907 /*
1908 * ":let" without "=": list variables
1909 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910 if (*arg == '[')
1911 EMSG(_(e_invarg));
1912 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001914 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001916 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001918 list_glob_vars(&first);
1919 list_buf_vars(&first);
1920 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001921#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001922 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001923#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001924 list_script_vars(&first);
1925 list_func_vars(&first);
1926 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 eap->nextcmd = check_nextcmd(arg);
1929 }
1930 else
1931 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 op[0] = '=';
1933 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001934 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001936 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1937 op[0] = *expr; /* +=, -= or .= */
1938 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001940 else
1941 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (eap->skip)
1944 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (eap->skip)
1947 {
1948 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 --emsg_skip;
1951 }
1952 else if (i != FAIL)
1953 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001955 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 }
1958 }
1959}
1960
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961/*
1962 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1963 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 * When "nextchars" is not NULL it points to a string with characters that
1965 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1966 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 * Returns OK or FAIL;
1968 */
1969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001970ex_let_vars(
1971 char_u *arg_start,
1972 typval_T *tv,
1973 int copy, /* copy values from "tv", don't move */
1974 int semicolon, /* from skip_var_list() */
1975 int var_count, /* from skip_var_list() */
1976 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977{
1978 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001979 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001981 listitem_T *item;
1982 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983
1984 if (*arg != '[')
1985 {
1986 /*
1987 * ":let var = expr" or ":for var in list"
1988 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 return FAIL;
1991 return OK;
1992 }
1993
1994 /*
1995 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1996 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001997 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 {
1999 EMSG(_(e_listreq));
2000 return FAIL;
2001 }
2002
2003 i = list_len(l);
2004 if (semicolon == 0 && var_count < i)
2005 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002006 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 return FAIL;
2008 }
2009 if (var_count - semicolon > i)
2010 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002011 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 return FAIL;
2013 }
2014
2015 item = l->lv_first;
2016 while (*arg != ']')
2017 {
2018 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 item = item->li_next;
2021 if (arg == NULL)
2022 return FAIL;
2023
2024 arg = skipwhite(arg);
2025 if (*arg == ';')
2026 {
2027 /* Put the rest of the list (may be empty) in the var after ';'.
2028 * Create a new list for this. */
2029 l = list_alloc();
2030 if (l == NULL)
2031 return FAIL;
2032 while (item != NULL)
2033 {
2034 list_append_tv(l, &item->li_tv);
2035 item = item->li_next;
2036 }
2037
2038 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002039 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 ltv.vval.v_list = l;
2041 l->lv_refcount = 1;
2042
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2044 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002045 clear_tv(&ltv);
2046 if (arg == NULL)
2047 return FAIL;
2048 break;
2049 }
2050 else if (*arg != ',' && *arg != ']')
2051 {
2052 EMSG2(_(e_intern2), "ex_let_vars()");
2053 return FAIL;
2054 }
2055 }
2056
2057 return OK;
2058}
2059
2060/*
2061 * Skip over assignable variable "var" or list of variables "[var, var]".
2062 * Used for ":let varvar = expr" and ":for varvar in expr".
2063 * For "[var, var]" increment "*var_count" for each variable.
2064 * for "[var, var; var]" set "semicolon".
2065 * Return NULL for an error.
2066 */
2067 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002068skip_var_list(
2069 char_u *arg,
2070 int *var_count,
2071 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072{
2073 char_u *p, *s;
2074
2075 if (*arg == '[')
2076 {
2077 /* "[var, var]": find the matching ']'. */
2078 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002079 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002080 {
2081 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2082 s = skip_var_one(p);
2083 if (s == p)
2084 {
2085 EMSG2(_(e_invarg2), p);
2086 return NULL;
2087 }
2088 ++*var_count;
2089
2090 p = skipwhite(s);
2091 if (*p == ']')
2092 break;
2093 else if (*p == ';')
2094 {
2095 if (*semicolon == 1)
2096 {
2097 EMSG(_("Double ; in list of variables"));
2098 return NULL;
2099 }
2100 *semicolon = 1;
2101 }
2102 else if (*p != ',')
2103 {
2104 EMSG2(_(e_invarg2), p);
2105 return NULL;
2106 }
2107 }
2108 return p + 1;
2109 }
2110 else
2111 return skip_var_one(arg);
2112}
2113
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002115 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002116 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002118 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002119skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002120{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002121 if (*arg == '@' && arg[1] != NUL)
2122 return arg + 2;
2123 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2124 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002125}
2126
Bram Moolenaara7043832005-01-21 11:56:39 +00002127/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002128 * List variables for hashtab "ht" with prefix "prefix".
2129 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002130 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002132list_hashtable_vars(
2133 hashtab_T *ht,
2134 char_u *prefix,
2135 int empty,
2136 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002137{
Bram Moolenaar33570922005-01-25 22:26:29 +00002138 hashitem_T *hi;
2139 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002140 int todo;
2141
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002142 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002143 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2144 {
2145 if (!HASHITEM_EMPTY(hi))
2146 {
2147 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002148 di = HI2DI(hi);
2149 if (empty || di->di_tv.v_type != VAR_STRING
2150 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002152 }
2153 }
2154}
2155
2156/*
2157 * List global variables.
2158 */
2159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002160list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002161{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002163}
2164
2165/*
2166 * List buffer variables.
2167 */
2168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002169list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002170{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002171 char_u numbuf[NUMBUFLEN];
2172
Bram Moolenaar429fa852013-04-15 12:27:36 +02002173 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175
2176 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2178 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002179}
2180
2181/*
2182 * List window variables.
2183 */
2184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002185list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002186{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002187 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002189}
2190
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002191#ifdef FEAT_WINDOWS
2192/*
2193 * List tab page variables.
2194 */
2195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002196list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002197{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002198 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002200}
2201#endif
2202
Bram Moolenaara7043832005-01-21 11:56:39 +00002203/*
2204 * List Vim variables.
2205 */
2206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002207list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210}
2211
2212/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002213 * List script-local variables, if there is a script.
2214 */
2215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002216list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002217{
2218 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2220 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002221}
2222
2223/*
2224 * List function variables, if there is a function.
2225 */
2226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002228{
2229 if (current_funccal != NULL)
2230 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002232}
2233
2234/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 * List variables in "arg".
2236 */
2237 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239{
2240 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 char_u *name_start;
2244 char_u *arg_subsc;
2245 char_u *tofree;
2246 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247
2248 while (!ends_excmd(*arg) && !got_int)
2249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002252 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2254 {
2255 emsg_severe = TRUE;
2256 EMSG(_(e_trailing));
2257 break;
2258 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 /* get_name_len() takes care of expanding curly braces */
2263 name_start = name = arg;
2264 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2265 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 /* This is mainly to keep test 49 working: when expanding
2268 * curly braces fails overrule the exception error message. */
2269 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 emsg_severe = TRUE;
2272 EMSG2(_(e_invarg2), arg);
2273 break;
2274 }
2275 error = TRUE;
2276 }
2277 else
2278 {
2279 if (tofree != NULL)
2280 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002281 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 else
2284 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 /* handle d.key, l[idx], f(expr) */
2286 arg_subsc = arg;
2287 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002288 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002295 case 'g': list_glob_vars(first); break;
2296 case 'b': list_buf_vars(first); break;
2297 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002298#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002300#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002301 case 'v': list_vim_vars(first); break;
2302 case 's': list_script_vars(first); break;
2303 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 default:
2305 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002307 }
2308 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309 {
2310 char_u numbuf[NUMBUFLEN];
2311 char_u *tf;
2312 int c;
2313 char_u *s;
2314
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002315 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 c = *arg;
2317 *arg = NUL;
2318 list_one_var_a((char_u *)"",
2319 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002320 tv.v_type,
2321 s == NULL ? (char_u *)"" : s,
2322 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323 *arg = c;
2324 vim_free(tf);
2325 }
2326 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002327 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 }
2329 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002330
2331 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333
2334 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
2336
2337 return arg;
2338}
2339
2340/*
2341 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2342 * Returns a pointer to the char just after the var name.
2343 * Returns NULL if there is an error.
2344 */
2345 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002346ex_let_one(
2347 char_u *arg, /* points to variable name */
2348 typval_T *tv, /* value to assign to variable */
2349 int copy, /* copy value from "tv" */
2350 char_u *endchars, /* valid chars after variable name or NULL */
2351 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352{
2353 int c1;
2354 char_u *name;
2355 char_u *p;
2356 char_u *arg_end = NULL;
2357 int len;
2358 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360
2361 /*
2362 * ":let $VAR = expr": Set environment variable.
2363 */
2364 if (*arg == '$')
2365 {
2366 /* Find the end of the name. */
2367 ++arg;
2368 name = arg;
2369 len = get_env_len(&arg);
2370 if (len == 0)
2371 EMSG2(_(e_invarg2), name - 1);
2372 else
2373 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002374 if (op != NULL && (*op == '+' || *op == '-'))
2375 EMSG2(_(e_letwrong), op);
2376 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002377 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002379 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 {
2381 c1 = name[len];
2382 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002383 p = get_tv_string_chk(tv);
2384 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 {
2386 int mustfree = FALSE;
2387 char_u *s = vim_getenv(name, &mustfree);
2388
2389 if (s != NULL)
2390 {
2391 p = tofree = concat_str(s, p);
2392 if (mustfree)
2393 vim_free(s);
2394 }
2395 }
2396 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002397 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002399 if (STRICMP(name, "HOME") == 0)
2400 init_homedir();
2401 else if (didset_vim && STRICMP(name, "VIM") == 0)
2402 didset_vim = FALSE;
2403 else if (didset_vimruntime
2404 && STRICMP(name, "VIMRUNTIME") == 0)
2405 didset_vimruntime = FALSE;
2406 arg_end = arg;
2407 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002408 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 }
2411 }
2412 }
2413
2414 /*
2415 * ":let &option = expr": Set option value.
2416 * ":let &l:option = expr": Set local option value.
2417 * ":let &g:option = expr": Set global option value.
2418 */
2419 else if (*arg == '&')
2420 {
2421 /* Find the end of the name. */
2422 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002423 if (p == NULL || (endchars != NULL
2424 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 EMSG(_(e_letunexp));
2426 else
2427 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 long n;
2429 int opt_type;
2430 long numval;
2431 char_u *stringval = NULL;
2432 char_u *s;
2433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 c1 = *p;
2435 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436
2437 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 s = get_tv_string_chk(tv); /* != NULL if number or string */
2439 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 {
2441 opt_type = get_option_value(arg, &numval,
2442 &stringval, opt_flags);
2443 if ((opt_type == 1 && *op == '.')
2444 || (opt_type == 0 && *op != '.'))
2445 EMSG2(_(e_letwrong), op);
2446 else
2447 {
2448 if (opt_type == 1) /* number */
2449 {
2450 if (*op == '+')
2451 n = numval + n;
2452 else
2453 n = numval - n;
2454 }
2455 else if (opt_type == 0 && stringval != NULL) /* string */
2456 {
2457 s = concat_str(stringval, s);
2458 vim_free(stringval);
2459 stringval = s;
2460 }
2461 }
2462 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 if (s != NULL)
2464 {
2465 set_option_value(arg, n, s, opt_flags);
2466 arg_end = p;
2467 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 }
2471 }
2472
2473 /*
2474 * ":let @r = expr": Set register contents.
2475 */
2476 else if (*arg == '@')
2477 {
2478 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 if (op != NULL && (*op == '+' || *op == '-'))
2480 EMSG2(_(e_letwrong), op);
2481 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002482 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 EMSG(_(e_letunexp));
2484 else
2485 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002486 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002487 char_u *s;
2488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002489 p = get_tv_string_chk(tv);
2490 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002492 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 if (s != NULL)
2494 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002495 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 vim_free(s);
2497 }
2498 }
2499 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502 arg_end = arg + 1;
2503 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002504 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
2506 }
2507
2508 /*
2509 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002512 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002514 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002516 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2520 EMSG(_(e_letunexp));
2521 else
2522 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 arg_end = p;
2525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 }
2529
2530 else
2531 EMSG2(_(e_invarg2), arg);
2532
2533 return arg_end;
2534}
2535
2536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2538 */
2539 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002540check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541{
2542 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2543 {
2544 EMSG2(_(e_readonlyvar), arg);
2545 return TRUE;
2546 }
2547 return FALSE;
2548}
2549
2550/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 * Get an lval: variable, Dict item or List item that can be assigned a value
2552 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2553 * "name.key", "name.key[expr]" etc.
2554 * Indexing only works if "name" is an existing List or Dictionary.
2555 * "name" points to the start of the name.
2556 * If "rettv" is not NULL it points to the value to be assigned.
2557 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2558 * wrong; must end in space or cmd separator.
2559 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002560 * flags:
2561 * GLV_QUIET: do not give error messages
2562 * GLV_NO_AUTOLOAD: do not use script autoloading
2563 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002565 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 */
2568 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002569get_lval(
2570 char_u *name,
2571 typval_T *rettv,
2572 lval_T *lp,
2573 int unlet,
2574 int skip,
2575 int flags, /* GLV_ values */
2576 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 char_u *expr_start, *expr_end;
2580 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 dictitem_T *v;
2582 typval_T var1;
2583 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002589 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593
2594 if (skip)
2595 {
2596 /* When skipping just find the end of the name. */
2597 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002598 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 }
2600
2601 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002602 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (expr_start != NULL)
2604 {
2605 /* Don't expand the name when we already know there is an error. */
2606 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2607 && *p != '[' && *p != '.')
2608 {
2609 EMSG(_(e_trailing));
2610 return NULL;
2611 }
2612
2613 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2614 if (lp->ll_exp_name == NULL)
2615 {
2616 /* Report an invalid expression in braces, unless the
2617 * expression evaluation has been cancelled due to an
2618 * aborting error, an interrupt, or an exception. */
2619 if (!aborting() && !quiet)
2620 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002621 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 EMSG2(_(e_invarg2), name);
2623 return NULL;
2624 }
2625 }
2626 lp->ll_name = lp->ll_exp_name;
2627 }
2628 else
2629 lp->ll_name = name;
2630
2631 /* Without [idx] or .key we are done. */
2632 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2633 return p;
2634
2635 cc = *p;
2636 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002637 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (v == NULL && !quiet)
2639 EMSG2(_(e_undefvar), lp->ll_name);
2640 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 if (v == NULL)
2642 return NULL;
2643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 /*
2645 * Loop until no more [idx] or .key is following.
2646 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002647 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2651 && !(lp->ll_tv->v_type == VAR_DICT
2652 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!quiet)
2655 EMSG(_("E689: Can only index a List or Dictionary"));
2656 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002657 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!quiet)
2661 EMSG(_("E708: [:] must come last"));
2662 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 len = -1;
2666 if (*p == '.')
2667 {
2668 key = p + 1;
2669 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2670 ;
2671 if (len == 0)
2672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (!quiet)
2674 EMSG(_(e_emptykey));
2675 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
2677 p = key + len;
2678 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 else
2680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002682 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 if (*p == ':')
2684 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 else
2686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 empty1 = FALSE;
2688 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002690 if (get_tv_string_chk(&var1) == NULL)
2691 {
2692 /* not a number or string */
2693 clear_tv(&var1);
2694 return NULL;
2695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
2697
2698 /* Optionally get the second index [ :expr]. */
2699 if (*p == ':')
2700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002704 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705 if (!empty1)
2706 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (rettv != NULL && (rettv->v_type != VAR_LIST
2710 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
2713 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 if (!empty1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 p = skipwhite(p + 1);
2719 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
2722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2725 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (!empty1)
2727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002730 if (get_tv_string_chk(&var2) == NULL)
2731 {
2732 /* not a number or string */
2733 if (!empty1)
2734 clear_tv(&var1);
2735 clear_tv(&var2);
2736 return NULL;
2737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002743
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
2747 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (!empty1)
2749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 }
2754
2755 /* Skip to past ']'. */
2756 ++p;
2757 }
2758
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
2761 if (len == -1)
2762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002764 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (*key == NUL)
2766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (!quiet)
2768 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
2772 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002773 lp->ll_list = NULL;
2774 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002775 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002776
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002777 /* When assigning to a scope dictionary check that a function and
2778 * variable name is valid (only variable name unless it is l: or
2779 * g: dictionary). Disallow overwriting a builtin function. */
2780 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002781 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002782 int prevval;
2783 int wrong;
2784
2785 if (len != -1)
2786 {
2787 prevval = key[len];
2788 key[len] = NUL;
2789 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002790 else
2791 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002792 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2793 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002794 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002795 || !valid_varname(key);
2796 if (len != -1)
2797 key[len] = prevval;
2798 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002799 return NULL;
2800 }
2801
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002804 /* Can't add "v:" variable. */
2805 if (lp->ll_dict == &vimvardict)
2806 {
2807 EMSG2(_(e_illvar), name);
2808 return NULL;
2809 }
2810
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002811 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002815 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (len == -1)
2817 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 }
2820 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 if (len == -1)
2825 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 p = NULL;
2828 break;
2829 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002830 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002831 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002832 return NULL;
2833
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 if (len == -1)
2835 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 }
2838 else
2839 {
2840 /*
2841 * Get the number and item for the only or first index of the List.
2842 */
2843 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 else
2846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002847 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 clear_tv(&var1);
2849 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002850 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 lp->ll_list = lp->ll_tv->vval.v_list;
2852 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2853 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002855 if (lp->ll_n1 < 0)
2856 {
2857 lp->ll_n1 = 0;
2858 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2859 }
2860 }
2861 if (lp->ll_li == NULL)
2862 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002865 if (!quiet)
2866 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 }
2869
2870 /*
2871 * May need to find the item or absolute index for the second
2872 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 * When no index given: "lp->ll_empty2" is TRUE.
2874 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002878 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 {
2885 if (!quiet)
2886 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 }
2891
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2893 if (lp->ll_n1 < 0)
2894 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2895 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002896 {
2897 if (!quiet)
2898 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002900 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002901 }
2902
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002905 }
2906
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 return p;
2908}
2909
2910/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002911 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 */
2913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915{
2916 vim_free(lp->ll_exp_name);
2917 vim_free(lp->ll_newkey);
2918}
2919
2920/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002921 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002923 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 */
2925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926set_var_lval(
2927 lval_T *lp,
2928 char_u *endp,
2929 typval_T *rettv,
2930 int copy,
2931 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932{
2933 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 listitem_T *ri;
2935 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936
2937 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 cc = *endp;
2942 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943 if (op != NULL && *op != '=')
2944 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002945 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002947 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002948 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002949 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002950 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002952 if ((di == NULL
2953 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2954 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2955 FALSE)))
2956 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 set_var(lp->ll_name, &tv, FALSE);
2958 clear_tv(&tv);
2959 }
2960 }
2961 else
2962 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 else if (tv_check_lock(lp->ll_newkey == NULL
2967 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002968 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002969 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 else if (lp->ll_range)
2971 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002972 listitem_T *ll_li = lp->ll_li;
2973 int ll_n1 = lp->ll_n1;
2974
2975 /*
2976 * Check whether any of the list items is locked
2977 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002978 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002979 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002980 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002981 return;
2982 ri = ri->li_next;
2983 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2984 break;
2985 ll_li = ll_li->li_next;
2986 ++ll_n1;
2987 }
2988
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 /*
2990 * Assign the List values to the list items.
2991 */
2992 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002993 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 if (op != NULL && *op != '=')
2995 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2996 else
2997 {
2998 clear_tv(&lp->ll_li->li_tv);
2999 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3000 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 ri = ri->li_next;
3002 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3003 break;
3004 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003007 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 ri = NULL;
3010 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 lp->ll_li = lp->ll_li->li_next;
3014 ++lp->ll_n1;
3015 }
3016 if (ri != NULL)
3017 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003018 else if (lp->ll_empty2
3019 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 : lp->ll_n1 != lp->ll_n2)
3021 EMSG(_("E711: List value has not enough items"));
3022 }
3023 else
3024 {
3025 /*
3026 * Assign to a List or Dictionary item.
3027 */
3028 if (lp->ll_newkey != NULL)
3029 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003030 if (op != NULL && *op != '=')
3031 {
3032 EMSG2(_(e_letwrong), op);
3033 return;
3034 }
3035
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003036 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003037 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003038 if (di == NULL)
3039 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003040 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3041 {
3042 vim_free(di);
3043 return;
3044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003046 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003047 else if (op != NULL && *op != '=')
3048 {
3049 tv_op(lp->ll_tv, rettv, op);
3050 return;
3051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 /*
3056 * Assign the value to the variable or list item.
3057 */
3058 if (copy)
3059 copy_tv(rettv, lp->ll_tv);
3060 else
3061 {
3062 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003063 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003065 }
3066 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003067}
3068
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003069/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3071 * Returns OK or FAIL.
3072 */
3073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003074tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075{
3076 long n;
3077 char_u numbuf[NUMBUFLEN];
3078 char_u *s;
3079
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003080 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3081 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3082 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003083 {
3084 switch (tv1->v_type)
3085 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003086 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 case VAR_DICT:
3088 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003089 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003090 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003091 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003092 break;
3093
3094 case VAR_LIST:
3095 if (*op != '+' || tv2->v_type != VAR_LIST)
3096 break;
3097 /* List += List */
3098 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3099 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3100 return OK;
3101
3102 case VAR_NUMBER:
3103 case VAR_STRING:
3104 if (tv2->v_type == VAR_LIST)
3105 break;
3106 if (*op == '+' || *op == '-')
3107 {
3108 /* nr += nr or nr -= nr*/
3109 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003110#ifdef FEAT_FLOAT
3111 if (tv2->v_type == VAR_FLOAT)
3112 {
3113 float_T f = n;
3114
3115 if (*op == '+')
3116 f += tv2->vval.v_float;
3117 else
3118 f -= tv2->vval.v_float;
3119 clear_tv(tv1);
3120 tv1->v_type = VAR_FLOAT;
3121 tv1->vval.v_float = f;
3122 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124#endif
3125 {
3126 if (*op == '+')
3127 n += get_tv_number(tv2);
3128 else
3129 n -= get_tv_number(tv2);
3130 clear_tv(tv1);
3131 tv1->v_type = VAR_NUMBER;
3132 tv1->vval.v_number = n;
3133 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003134 }
3135 else
3136 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003137 if (tv2->v_type == VAR_FLOAT)
3138 break;
3139
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003140 /* str .= str */
3141 s = get_tv_string(tv1);
3142 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3143 clear_tv(tv1);
3144 tv1->v_type = VAR_STRING;
3145 tv1->vval.v_string = s;
3146 }
3147 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003148
3149#ifdef FEAT_FLOAT
3150 case VAR_FLOAT:
3151 {
3152 float_T f;
3153
3154 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3155 && tv2->v_type != VAR_NUMBER
3156 && tv2->v_type != VAR_STRING))
3157 break;
3158 if (tv2->v_type == VAR_FLOAT)
3159 f = tv2->vval.v_float;
3160 else
3161 f = get_tv_number(tv2);
3162 if (*op == '+')
3163 tv1->vval.v_float += f;
3164 else
3165 tv1->vval.v_float -= f;
3166 }
3167 return OK;
3168#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003169 }
3170 }
3171
3172 EMSG2(_(e_letwrong), op);
3173 return FAIL;
3174}
3175
3176/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 * Add a watcher to a list.
3178 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003179 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003180list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181{
3182 lw->lw_next = l->lv_watch;
3183 l->lv_watch = lw;
3184}
3185
3186/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003187 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 * No warning when it isn't found...
3189 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003191list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192{
Bram Moolenaar33570922005-01-25 22:26:29 +00003193 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194
3195 lwp = &l->lv_watch;
3196 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3197 {
3198 if (lw == lwrem)
3199 {
3200 *lwp = lw->lw_next;
3201 break;
3202 }
3203 lwp = &lw->lw_next;
3204 }
3205}
3206
3207/*
3208 * Just before removing an item from a list: advance watchers to the next
3209 * item.
3210 */
3211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003212list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213{
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215
3216 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3217 if (lw->lw_item == item)
3218 lw->lw_item = item->li_next;
3219}
3220
3221/*
3222 * Evaluate the expression used in a ":for var in expr" command.
3223 * "arg" points to "var".
3224 * Set "*errp" to TRUE for an error, FALSE otherwise;
3225 * Return a pointer that holds the info. Null when there is an error.
3226 */
3227 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003228eval_for_line(
3229 char_u *arg,
3230 int *errp,
3231 char_u **nextcmdp,
3232 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003236 typval_T tv;
3237 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238
3239 *errp = TRUE; /* default: there is an error */
3240
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 if (fi == NULL)
3243 return NULL;
3244
3245 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3246 if (expr == NULL)
3247 return fi;
3248
3249 expr = skipwhite(expr);
3250 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3251 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003252 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 return fi;
3254 }
3255
3256 if (skip)
3257 ++emsg_skip;
3258 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3259 {
3260 *errp = FALSE;
3261 if (!skip)
3262 {
3263 l = tv.vval.v_list;
3264 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003265 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003267 clear_tv(&tv);
3268 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 else
3270 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003271 /* No need to increment the refcount, it's already set for the
3272 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 fi->fi_list = l;
3274 list_add_watch(l, &fi->fi_lw);
3275 fi->fi_lw.lw_item = l->lv_first;
3276 }
3277 }
3278 }
3279 if (skip)
3280 --emsg_skip;
3281
3282 return fi;
3283}
3284
3285/*
3286 * Use the first item in a ":for" list. Advance to the next.
3287 * Assign the values to the variable (list). "arg" points to the first one.
3288 * Return TRUE when a valid item was found, FALSE when at end of list or
3289 * something wrong.
3290 */
3291 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003292next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003294 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297
3298 item = fi->fi_lw.lw_item;
3299 if (item == NULL)
3300 result = FALSE;
3301 else
3302 {
3303 fi->fi_lw.lw_item = item->li_next;
3304 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3305 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3306 }
3307 return result;
3308}
3309
3310/*
3311 * Free the structure used to store info used by ":for".
3312 */
3313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003314free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315{
Bram Moolenaar33570922005-01-25 22:26:29 +00003316 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003318 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003319 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003321 list_unref(fi->fi_list);
3322 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 vim_free(fi);
3324}
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3327
3328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003329set_context_for_expression(
3330 expand_T *xp,
3331 char_u *arg,
3332 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 int got_eq = FALSE;
3335 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338 if (cmdidx == CMD_let)
3339 {
3340 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003341 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 {
3343 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003344 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 {
3346 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003347 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 if (vim_iswhite(*p))
3349 break;
3350 }
3351 return;
3352 }
3353 }
3354 else
3355 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3356 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 while ((xp->xp_pattern = vim_strpbrk(arg,
3358 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3359 {
3360 c = *xp->xp_pattern;
3361 if (c == '&')
3362 {
3363 c = xp->xp_pattern[1];
3364 if (c == '&')
3365 {
3366 ++xp->xp_pattern;
3367 xp->xp_context = cmdidx != CMD_let || got_eq
3368 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3369 }
3370 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003373 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3374 xp->xp_pattern += 2;
3375
3376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378 else if (c == '$')
3379 {
3380 /* environment variable */
3381 xp->xp_context = EXPAND_ENV_VARS;
3382 }
3383 else if (c == '=')
3384 {
3385 got_eq = TRUE;
3386 xp->xp_context = EXPAND_EXPRESSION;
3387 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003388 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 && xp->xp_context == EXPAND_FUNCTIONS
3390 && vim_strchr(xp->xp_pattern, '(') == NULL)
3391 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003392 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 break;
3394 }
3395 else if (cmdidx != CMD_let || got_eq)
3396 {
3397 if (c == '"') /* string */
3398 {
3399 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3400 if (c == '\\' && xp->xp_pattern[1] != NUL)
3401 ++xp->xp_pattern;
3402 xp->xp_context = EXPAND_NOTHING;
3403 }
3404 else if (c == '\'') /* literal string */
3405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003406 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3408 /* skip */ ;
3409 xp->xp_context = EXPAND_NOTHING;
3410 }
3411 else if (c == '|')
3412 {
3413 if (xp->xp_pattern[1] == '|')
3414 {
3415 ++xp->xp_pattern;
3416 xp->xp_context = EXPAND_EXPRESSION;
3417 }
3418 else
3419 xp->xp_context = EXPAND_COMMANDS;
3420 }
3421 else
3422 xp->xp_context = EXPAND_EXPRESSION;
3423 }
3424 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003425 /* Doesn't look like something valid, expand as an expression
3426 * anyway. */
3427 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 arg = xp->xp_pattern;
3429 if (*arg != NUL)
3430 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3431 /* skip */ ;
3432 }
3433 xp->xp_pattern = arg;
3434}
3435
3436#endif /* FEAT_CMDL_COMPL */
3437
3438/*
3439 * ":1,25call func(arg1, arg2)" function call.
3440 */
3441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003442ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 char_u *arg = eap->arg;
3445 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003447 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003449 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 linenr_T lnum;
3451 int doesrange;
3452 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003453 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003455 if (eap->skip)
3456 {
3457 /* trans_function_name() doesn't work well when skipping, use eval0()
3458 * instead to skip to any following command, e.g. for:
3459 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003460 ++emsg_skip;
3461 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3462 clear_tv(&rettv);
3463 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003464 return;
3465 }
3466
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003467 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003468 if (fudi.fd_newkey != NULL)
3469 {
3470 /* Still need to give an error message for missing key. */
3471 EMSG2(_(e_dictkey), fudi.fd_newkey);
3472 vim_free(fudi.fd_newkey);
3473 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003474 if (tofree == NULL)
3475 return;
3476
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003477 /* Increase refcount on dictionary, it could get deleted when evaluating
3478 * the arguments. */
3479 if (fudi.fd_dict != NULL)
3480 ++fudi.fd_dict->dv_refcount;
3481
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003482 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003483 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003484 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
Bram Moolenaar532c7802005-01-27 14:44:31 +00003486 /* Skip white space to allow ":call func ()". Not good, but required for
3487 * backward compatibility. */
3488 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003489 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
3491 if (*startarg != '(')
3492 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003493 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 goto end;
3495 }
3496
3497 /*
3498 * When skipping, evaluate the function once, to find the end of the
3499 * arguments.
3500 * When the function takes a range, this is discovered after the first
3501 * call, and the loop is broken.
3502 */
3503 if (eap->skip)
3504 {
3505 ++emsg_skip;
3506 lnum = eap->line2; /* do it once, also with an invalid range */
3507 }
3508 else
3509 lnum = eap->line1;
3510 for ( ; lnum <= eap->line2; ++lnum)
3511 {
3512 if (!eap->skip && eap->addr_count > 0)
3513 {
3514 curwin->w_cursor.lnum = lnum;
3515 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003516#ifdef FEAT_VIRTUALEDIT
3517 curwin->w_cursor.coladd = 0;
3518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003521 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003522 eap->line1, eap->line2, &doesrange,
3523 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
3525 failed = TRUE;
3526 break;
3527 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003528
3529 /* Handle a function returning a Funcref, Dictionary or List. */
3530 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3531 {
3532 failed = TRUE;
3533 break;
3534 }
3535
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003536 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (doesrange || eap->skip)
3538 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003541 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003543 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (aborting())
3545 break;
3546 }
3547 if (eap->skip)
3548 --emsg_skip;
3549
3550 if (!failed)
3551 {
3552 /* Check for trailing illegal characters and a following command. */
3553 if (!ends_excmd(*arg))
3554 {
3555 emsg_severe = TRUE;
3556 EMSG(_(e_trailing));
3557 }
3558 else
3559 eap->nextcmd = check_nextcmd(arg);
3560 }
3561
3562end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003563 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003564 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565}
3566
3567/*
3568 * ":unlet[!] var1 ... " command.
3569 */
3570 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003571ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003573 ex_unletlock(eap, eap->arg, 0);
3574}
3575
3576/*
3577 * ":lockvar" and ":unlockvar" commands
3578 */
3579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003580ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003581{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583 int deep = 2;
3584
3585 if (eap->forceit)
3586 deep = -1;
3587 else if (vim_isdigit(*arg))
3588 {
3589 deep = getdigits(&arg);
3590 arg = skipwhite(arg);
3591 }
3592
3593 ex_unletlock(eap, arg, deep);
3594}
3595
3596/*
3597 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3598 */
3599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003600ex_unletlock(
3601 exarg_T *eap,
3602 char_u *argstart,
3603 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003604{
3605 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003608 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
3610 do
3611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003613 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003614 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 if (lv.ll_name == NULL)
3616 error = TRUE; /* error but continue parsing */
3617 if (name_end == NULL || (!vim_iswhite(*name_end)
3618 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 if (name_end != NULL)
3621 {
3622 emsg_severe = TRUE;
3623 EMSG(_(e_trailing));
3624 }
3625 if (!(eap->skip || error))
3626 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 break;
3628 }
3629
3630 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003631 {
3632 if (eap->cmdidx == CMD_unlet)
3633 {
3634 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3635 error = TRUE;
3636 }
3637 else
3638 {
3639 if (do_lock_var(&lv, name_end, deep,
3640 eap->cmdidx == CMD_lockvar) == FAIL)
3641 error = TRUE;
3642 }
3643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 if (!eap->skip)
3646 clear_lval(&lv);
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 arg = skipwhite(name_end);
3649 } while (!ends_excmd(*arg));
3650
3651 eap->nextcmd = check_nextcmd(arg);
3652}
3653
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655do_unlet_var(
3656 lval_T *lp,
3657 char_u *name_end,
3658 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 int ret = OK;
3661 int cc;
3662
3663 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 cc = *name_end;
3666 *name_end = NUL;
3667
3668 /* Normal name or expanded name. */
3669 if (check_changedtick(lp->ll_name))
3670 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003671 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003673 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003674 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003675 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003676 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003677 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003678 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 else if (lp->ll_range)
3681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003683 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003684 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003685
3686 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3687 {
3688 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003689 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690 return FAIL;
3691 ll_li = li;
3692 ++ll_n1;
3693 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694
3695 /* Delete a range of List items. */
3696 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3697 {
3698 li = lp->ll_li->li_next;
3699 listitem_remove(lp->ll_list, lp->ll_li);
3700 lp->ll_li = li;
3701 ++lp->ll_n1;
3702 }
3703 }
3704 else
3705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 /* unlet a List item. */
3708 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003711 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 }
3713
3714 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003715}
3716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717/*
3718 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003719 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 */
3721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003722do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
Bram Moolenaar33570922005-01-25 22:26:29 +00003724 hashtab_T *ht;
3725 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003726 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003727 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003728 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 ht = find_var_ht(name, &varname);
3731 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003733 if (ht == &globvarht)
3734 d = &globvardict;
3735 else if (current_funccal != NULL
3736 && ht == &current_funccal->l_vars.dv_hashtab)
3737 d = &current_funccal->l_vars;
3738 else if (ht == &compat_hashtab)
3739 d = &vimvardict;
3740 else
3741 {
3742 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3743 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3744 }
3745 if (d == NULL)
3746 {
3747 EMSG2(_(e_intern2), "do_unlet()");
3748 return FAIL;
3749 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003750 hi = hash_find(ht, varname);
3751 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003752 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003753 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003754 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003755 || var_check_ro(di->di_flags, name, FALSE)
3756 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003757 return FAIL;
3758
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003759 delete_var(ht, hi);
3760 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003763 if (forceit)
3764 return OK;
3765 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 return FAIL;
3767}
3768
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003769/*
3770 * Lock or unlock variable indicated by "lp".
3771 * "deep" is the levels to go (-1 for unlimited);
3772 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3773 */
3774 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003775do_lock_var(
3776 lval_T *lp,
3777 char_u *name_end,
3778 int deep,
3779 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003780{
3781 int ret = OK;
3782 int cc;
3783 dictitem_T *di;
3784
3785 if (deep == 0) /* nothing to do */
3786 return OK;
3787
3788 if (lp->ll_tv == NULL)
3789 {
3790 cc = *name_end;
3791 *name_end = NUL;
3792
3793 /* Normal name or expanded name. */
3794 if (check_changedtick(lp->ll_name))
3795 ret = FAIL;
3796 else
3797 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003798 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003799 if (di == NULL)
3800 ret = FAIL;
3801 else
3802 {
3803 if (lock)
3804 di->di_flags |= DI_FLAGS_LOCK;
3805 else
3806 di->di_flags &= ~DI_FLAGS_LOCK;
3807 item_lock(&di->di_tv, deep, lock);
3808 }
3809 }
3810 *name_end = cc;
3811 }
3812 else if (lp->ll_range)
3813 {
3814 listitem_T *li = lp->ll_li;
3815
3816 /* (un)lock a range of List items. */
3817 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3818 {
3819 item_lock(&li->li_tv, deep, lock);
3820 li = li->li_next;
3821 ++lp->ll_n1;
3822 }
3823 }
3824 else if (lp->ll_list != NULL)
3825 /* (un)lock a List item. */
3826 item_lock(&lp->ll_li->li_tv, deep, lock);
3827 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003828 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003829 item_lock(&lp->ll_di->di_tv, deep, lock);
3830
3831 return ret;
3832}
3833
3834/*
3835 * Lock or unlock an item. "deep" is nr of levels to go.
3836 */
3837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003838item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003839{
3840 static int recurse = 0;
3841 list_T *l;
3842 listitem_T *li;
3843 dict_T *d;
3844 hashitem_T *hi;
3845 int todo;
3846
3847 if (recurse >= DICT_MAXNEST)
3848 {
3849 EMSG(_("E743: variable nested too deep for (un)lock"));
3850 return;
3851 }
3852 if (deep == 0)
3853 return;
3854 ++recurse;
3855
3856 /* lock/unlock the item itself */
3857 if (lock)
3858 tv->v_lock |= VAR_LOCKED;
3859 else
3860 tv->v_lock &= ~VAR_LOCKED;
3861
3862 switch (tv->v_type)
3863 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003864 case VAR_UNKNOWN:
3865 case VAR_NUMBER:
3866 case VAR_STRING:
3867 case VAR_FUNC:
3868 case VAR_FLOAT:
3869 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003870 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003871 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003872 break;
3873
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003874 case VAR_LIST:
3875 if ((l = tv->vval.v_list) != NULL)
3876 {
3877 if (lock)
3878 l->lv_lock |= VAR_LOCKED;
3879 else
3880 l->lv_lock &= ~VAR_LOCKED;
3881 if (deep < 0 || deep > 1)
3882 /* recursive: lock/unlock the items the List contains */
3883 for (li = l->lv_first; li != NULL; li = li->li_next)
3884 item_lock(&li->li_tv, deep - 1, lock);
3885 }
3886 break;
3887 case VAR_DICT:
3888 if ((d = tv->vval.v_dict) != NULL)
3889 {
3890 if (lock)
3891 d->dv_lock |= VAR_LOCKED;
3892 else
3893 d->dv_lock &= ~VAR_LOCKED;
3894 if (deep < 0 || deep > 1)
3895 {
3896 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003897 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003898 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3899 {
3900 if (!HASHITEM_EMPTY(hi))
3901 {
3902 --todo;
3903 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3904 }
3905 }
3906 }
3907 }
3908 }
3909 --recurse;
3910}
3911
Bram Moolenaara40058a2005-07-11 22:42:07 +00003912/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003913 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3914 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003915 */
3916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003917tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003918{
3919 return (tv->v_lock & VAR_LOCKED)
3920 || (tv->v_type == VAR_LIST
3921 && tv->vval.v_list != NULL
3922 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3923 || (tv->v_type == VAR_DICT
3924 && tv->vval.v_dict != NULL
3925 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3926}
3927
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3929/*
3930 * Delete all "menutrans_" variables.
3931 */
3932 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003933del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934{
Bram Moolenaar33570922005-01-25 22:26:29 +00003935 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003936 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003939 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003941 {
3942 if (!HASHITEM_EMPTY(hi))
3943 {
3944 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3946 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 }
3948 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950}
3951#endif
3952
3953#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3954
3955/*
3956 * Local string buffer for the next two functions to store a variable name
3957 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3958 * get_user_var_name().
3959 */
3960
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003961static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
3963static char_u *varnamebuf = NULL;
3964static int varnamebuflen = 0;
3965
3966/*
3967 * Function to concatenate a prefix and a variable name.
3968 */
3969 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003970cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971{
3972 int len;
3973
3974 len = (int)STRLEN(name) + 3;
3975 if (len > varnamebuflen)
3976 {
3977 vim_free(varnamebuf);
3978 len += 10; /* some additional space */
3979 varnamebuf = alloc(len);
3980 if (varnamebuf == NULL)
3981 {
3982 varnamebuflen = 0;
3983 return NULL;
3984 }
3985 varnamebuflen = len;
3986 }
3987 *varnamebuf = prefix;
3988 varnamebuf[1] = ':';
3989 STRCPY(varnamebuf + 2, name);
3990 return varnamebuf;
3991}
3992
3993/*
3994 * Function given to ExpandGeneric() to obtain the list of user defined
3995 * (global/buffer/window/built-in) variable names.
3996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003998get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004000 static long_u gdone;
4001 static long_u bdone;
4002 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004003#ifdef FEAT_WINDOWS
4004 static long_u tdone;
4005#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004006 static int vidx;
4007 static hashitem_T *hi;
4008 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004013#ifdef FEAT_WINDOWS
4014 tdone = 0;
4015#endif
4016 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004017
4018 /* Global variables */
4019 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004021 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004022 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004023 else
4024 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004025 while (HASHITEM_EMPTY(hi))
4026 ++hi;
4027 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4028 return cat_prefix_varname('g', hi->hi_key);
4029 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004031
4032 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004033 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004037 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004038 else
4039 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004040 while (HASHITEM_EMPTY(hi))
4041 ++hi;
4042 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 return (char_u *)"b:changedtick";
4048 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004049
4050 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004051 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004054 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 else
4057 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004058 while (HASHITEM_EMPTY(hi))
4059 ++hi;
4060 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004062
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004063#ifdef FEAT_WINDOWS
4064 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004065 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066 if (tdone < ht->ht_used)
4067 {
4068 if (tdone++ == 0)
4069 hi = ht->ht_array;
4070 else
4071 ++hi;
4072 while (HASHITEM_EMPTY(hi))
4073 ++hi;
4074 return cat_prefix_varname('t', hi->hi_key);
4075 }
4076#endif
4077
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 /* v: variables */
4079 if (vidx < VV_LEN)
4080 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
4082 vim_free(varnamebuf);
4083 varnamebuf = NULL;
4084 varnamebuflen = 0;
4085 return NULL;
4086}
4087
4088#endif /* FEAT_CMDL_COMPL */
4089
4090/*
4091 * types for expressions.
4092 */
4093typedef enum
4094{
4095 TYPE_UNKNOWN = 0
4096 , TYPE_EQUAL /* == */
4097 , TYPE_NEQUAL /* != */
4098 , TYPE_GREATER /* > */
4099 , TYPE_GEQUAL /* >= */
4100 , TYPE_SMALLER /* < */
4101 , TYPE_SEQUAL /* <= */
4102 , TYPE_MATCH /* =~ */
4103 , TYPE_NOMATCH /* !~ */
4104} exptype_T;
4105
4106/*
4107 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4110 */
4111
4112/*
4113 * Handle zero level expression.
4114 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004116 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 * Return OK or FAIL.
4118 */
4119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004120eval0(
4121 char_u *arg,
4122 typval_T *rettv,
4123 char_u **nextcmd,
4124 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
4126 int ret;
4127 char_u *p;
4128
4129 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 if (ret == FAIL || !ends_excmd(*p))
4132 {
4133 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 /*
4136 * Report the invalid expression unless the expression evaluation has
4137 * been cancelled due to an aborting error, an interrupt, or an
4138 * exception.
4139 */
4140 if (!aborting())
4141 EMSG2(_(e_invexpr2), arg);
4142 ret = FAIL;
4143 }
4144 if (nextcmd != NULL)
4145 *nextcmd = check_nextcmd(p);
4146
4147 return ret;
4148}
4149
4150/*
4151 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004152 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 *
4154 * "arg" must point to the first non-white of the expression.
4155 * "arg" is advanced to the next non-white after the recognized expression.
4156 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004157 * Note: "rettv.v_lock" is not set.
4158 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 * Return OK or FAIL.
4160 */
4161 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004162eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
4164 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166
4167 /*
4168 * Get the first variable.
4169 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 return FAIL;
4172
4173 if ((*arg)[0] == '?')
4174 {
4175 result = FALSE;
4176 if (evaluate)
4177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 int error = FALSE;
4179
4180 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004183 if (error)
4184 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186
4187 /*
4188 * Get the second variable.
4189 */
4190 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return FAIL;
4193
4194 /*
4195 * Check for the ":".
4196 */
4197 if ((*arg)[0] != ':')
4198 {
4199 EMSG(_("E109: Missing ':' after '?'"));
4200 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return FAIL;
4203 }
4204
4205 /*
4206 * Get the third variable.
4207 */
4208 *arg = skipwhite(*arg + 1);
4209 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4210 {
4211 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FAIL;
4214 }
4215 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218
4219 return OK;
4220}
4221
4222/*
4223 * Handle first level expression:
4224 * expr2 || expr2 || expr2 logical OR
4225 *
4226 * "arg" must point to the first non-white of the expression.
4227 * "arg" is advanced to the next non-white after the recognized expression.
4228 *
4229 * Return OK or FAIL.
4230 */
4231 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004232eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
Bram Moolenaar33570922005-01-25 22:26:29 +00004234 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 long result;
4236 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004237 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
4239 /*
4240 * Get the first variable.
4241 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 return FAIL;
4244
4245 /*
4246 * Repeat until there is no following "||".
4247 */
4248 first = TRUE;
4249 result = FALSE;
4250 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4251 {
4252 if (evaluate && first)
4253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 if (error)
4258 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 first = FALSE;
4260 }
4261
4262 /*
4263 * Get the second variable.
4264 */
4265 *arg = skipwhite(*arg + 2);
4266 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4267 return FAIL;
4268
4269 /*
4270 * Compute the result.
4271 */
4272 if (evaluate && !result)
4273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 if (error)
4278 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 if (evaluate)
4281 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 rettv->v_type = VAR_NUMBER;
4283 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 }
4286
4287 return OK;
4288}
4289
4290/*
4291 * Handle second level expression:
4292 * expr3 && expr3 && expr3 logical AND
4293 *
4294 * "arg" must point to the first non-white of the expression.
4295 * "arg" is advanced to the next non-white after the recognized expression.
4296 *
4297 * Return OK or FAIL.
4298 */
4299 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004300eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301{
Bram Moolenaar33570922005-01-25 22:26:29 +00004302 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 long result;
4304 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004305 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 /*
4308 * Get the first variable.
4309 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 return FAIL;
4312
4313 /*
4314 * Repeat until there is no following "&&".
4315 */
4316 first = TRUE;
4317 result = TRUE;
4318 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4319 {
4320 if (evaluate && first)
4321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 if (error)
4326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 first = FALSE;
4328 }
4329
4330 /*
4331 * Get the second variable.
4332 */
4333 *arg = skipwhite(*arg + 2);
4334 if (eval4(arg, &var2, evaluate && result) == FAIL)
4335 return FAIL;
4336
4337 /*
4338 * Compute the result.
4339 */
4340 if (evaluate && result)
4341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004344 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 if (error)
4346 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 if (evaluate)
4349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 rettv->v_type = VAR_NUMBER;
4351 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 }
4354
4355 return OK;
4356}
4357
4358/*
4359 * Handle third level expression:
4360 * var1 == var2
4361 * var1 =~ var2
4362 * var1 != var2
4363 * var1 !~ var2
4364 * var1 > var2
4365 * var1 >= var2
4366 * var1 < var2
4367 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004368 * var1 is var2
4369 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
4371 * "arg" must point to the first non-white of the expression.
4372 * "arg" is advanced to the next non-white after the recognized expression.
4373 *
4374 * Return OK or FAIL.
4375 */
4376 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004377eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378{
Bram Moolenaar33570922005-01-25 22:26:29 +00004379 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 char_u *p;
4381 int i;
4382 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004383 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 int len = 2;
4385 long n1, n2;
4386 char_u *s1, *s2;
4387 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4388 regmatch_T regmatch;
4389 int ic;
4390 char_u *save_cpo;
4391
4392 /*
4393 * Get the first variable.
4394 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004395 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 return FAIL;
4397
4398 p = *arg;
4399 switch (p[0])
4400 {
4401 case '=': if (p[1] == '=')
4402 type = TYPE_EQUAL;
4403 else if (p[1] == '~')
4404 type = TYPE_MATCH;
4405 break;
4406 case '!': if (p[1] == '=')
4407 type = TYPE_NEQUAL;
4408 else if (p[1] == '~')
4409 type = TYPE_NOMATCH;
4410 break;
4411 case '>': if (p[1] != '=')
4412 {
4413 type = TYPE_GREATER;
4414 len = 1;
4415 }
4416 else
4417 type = TYPE_GEQUAL;
4418 break;
4419 case '<': if (p[1] != '=')
4420 {
4421 type = TYPE_SMALLER;
4422 len = 1;
4423 }
4424 else
4425 type = TYPE_SEQUAL;
4426 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004427 case 'i': if (p[1] == 's')
4428 {
4429 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4430 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004431 i = p[len];
4432 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 {
4434 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4435 type_is = TRUE;
4436 }
4437 }
4438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440
4441 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004442 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 */
4444 if (type != TYPE_UNKNOWN)
4445 {
4446 /* extra question mark appended: ignore case */
4447 if (p[len] == '?')
4448 {
4449 ic = TRUE;
4450 ++len;
4451 }
4452 /* extra '#' appended: match case */
4453 else if (p[len] == '#')
4454 {
4455 ic = FALSE;
4456 ++len;
4457 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004458 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 else
4460 ic = p_ic;
4461
4462 /*
4463 * Get the second variable.
4464 */
4465 *arg = skipwhite(p + len);
4466 if (eval5(arg, &var2, evaluate) == FAIL)
4467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004468 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 return FAIL;
4470 }
4471
4472 if (evaluate)
4473 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004474 if (type_is && rettv->v_type != var2.v_type)
4475 {
4476 /* For "is" a different type always means FALSE, for "notis"
4477 * it means TRUE. */
4478 n1 = (type == TYPE_NEQUAL);
4479 }
4480 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4481 {
4482 if (type_is)
4483 {
4484 n1 = (rettv->v_type == var2.v_type
4485 && rettv->vval.v_list == var2.vval.v_list);
4486 if (type == TYPE_NEQUAL)
4487 n1 = !n1;
4488 }
4489 else if (rettv->v_type != var2.v_type
4490 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4491 {
4492 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004493 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004494 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004495 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004496 clear_tv(rettv);
4497 clear_tv(&var2);
4498 return FAIL;
4499 }
4500 else
4501 {
4502 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004503 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4504 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004505 if (type == TYPE_NEQUAL)
4506 n1 = !n1;
4507 }
4508 }
4509
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004510 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4511 {
4512 if (type_is)
4513 {
4514 n1 = (rettv->v_type == var2.v_type
4515 && rettv->vval.v_dict == var2.vval.v_dict);
4516 if (type == TYPE_NEQUAL)
4517 n1 = !n1;
4518 }
4519 else if (rettv->v_type != var2.v_type
4520 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4521 {
4522 if (rettv->v_type != var2.v_type)
4523 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4524 else
4525 EMSG(_("E736: Invalid operation for Dictionary"));
4526 clear_tv(rettv);
4527 clear_tv(&var2);
4528 return FAIL;
4529 }
4530 else
4531 {
4532 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004533 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4534 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004535 if (type == TYPE_NEQUAL)
4536 n1 = !n1;
4537 }
4538 }
4539
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004540 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4541 {
4542 if (rettv->v_type != var2.v_type
4543 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4544 {
4545 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004546 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004547 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004548 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004549 clear_tv(rettv);
4550 clear_tv(&var2);
4551 return FAIL;
4552 }
4553 else
4554 {
4555 /* Compare two Funcrefs for being equal or unequal. */
4556 if (rettv->vval.v_string == NULL
4557 || var2.vval.v_string == NULL)
4558 n1 = FALSE;
4559 else
4560 n1 = STRCMP(rettv->vval.v_string,
4561 var2.vval.v_string) == 0;
4562 if (type == TYPE_NEQUAL)
4563 n1 = !n1;
4564 }
4565 }
4566
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004567#ifdef FEAT_FLOAT
4568 /*
4569 * If one of the two variables is a float, compare as a float.
4570 * When using "=~" or "!~", always compare as string.
4571 */
4572 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4573 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4574 {
4575 float_T f1, f2;
4576
4577 if (rettv->v_type == VAR_FLOAT)
4578 f1 = rettv->vval.v_float;
4579 else
4580 f1 = get_tv_number(rettv);
4581 if (var2.v_type == VAR_FLOAT)
4582 f2 = var2.vval.v_float;
4583 else
4584 f2 = get_tv_number(&var2);
4585 n1 = FALSE;
4586 switch (type)
4587 {
4588 case TYPE_EQUAL: n1 = (f1 == f2); break;
4589 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4590 case TYPE_GREATER: n1 = (f1 > f2); break;
4591 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4592 case TYPE_SMALLER: n1 = (f1 < f2); break;
4593 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4594 case TYPE_UNKNOWN:
4595 case TYPE_MATCH:
4596 case TYPE_NOMATCH: break; /* avoid gcc warning */
4597 }
4598 }
4599#endif
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 /*
4602 * If one of the two variables is a number, compare as a number.
4603 * When using "=~" or "!~", always compare as string.
4604 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004605 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 n1 = get_tv_number(rettv);
4609 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 switch (type)
4611 {
4612 case TYPE_EQUAL: n1 = (n1 == n2); break;
4613 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4614 case TYPE_GREATER: n1 = (n1 > n2); break;
4615 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4616 case TYPE_SMALLER: n1 = (n1 < n2); break;
4617 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4618 case TYPE_UNKNOWN:
4619 case TYPE_MATCH:
4620 case TYPE_NOMATCH: break; /* avoid gcc warning */
4621 }
4622 }
4623 else
4624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 s1 = get_tv_string_buf(rettv, buf1);
4626 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4628 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4629 else
4630 i = 0;
4631 n1 = FALSE;
4632 switch (type)
4633 {
4634 case TYPE_EQUAL: n1 = (i == 0); break;
4635 case TYPE_NEQUAL: n1 = (i != 0); break;
4636 case TYPE_GREATER: n1 = (i > 0); break;
4637 case TYPE_GEQUAL: n1 = (i >= 0); break;
4638 case TYPE_SMALLER: n1 = (i < 0); break;
4639 case TYPE_SEQUAL: n1 = (i <= 0); break;
4640
4641 case TYPE_MATCH:
4642 case TYPE_NOMATCH:
4643 /* avoid 'l' flag in 'cpoptions' */
4644 save_cpo = p_cpo;
4645 p_cpo = (char_u *)"";
4646 regmatch.regprog = vim_regcomp(s2,
4647 RE_MAGIC + RE_STRING);
4648 regmatch.rm_ic = ic;
4649 if (regmatch.regprog != NULL)
4650 {
4651 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004652 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (type == TYPE_NOMATCH)
4654 n1 = !n1;
4655 }
4656 p_cpo = save_cpo;
4657 break;
4658
4659 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4660 }
4661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004662 clear_tv(rettv);
4663 clear_tv(&var2);
4664 rettv->v_type = VAR_NUMBER;
4665 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667 }
4668
4669 return OK;
4670}
4671
4672/*
4673 * Handle fourth level expression:
4674 * + number addition
4675 * - number subtraction
4676 * . string concatenation
4677 *
4678 * "arg" must point to the first non-white of the expression.
4679 * "arg" is advanced to the next non-white after the recognized expression.
4680 *
4681 * Return OK or FAIL.
4682 */
4683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004684eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
Bram Moolenaar33570922005-01-25 22:26:29 +00004686 typval_T var2;
4687 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 int op;
4689 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004690#ifdef FEAT_FLOAT
4691 float_T f1 = 0, f2 = 0;
4692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 char_u *s1, *s2;
4694 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4695 char_u *p;
4696
4697 /*
4698 * Get the first variable.
4699 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004700 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 return FAIL;
4702
4703 /*
4704 * Repeat computing, until no '+', '-' or '.' is following.
4705 */
4706 for (;;)
4707 {
4708 op = **arg;
4709 if (op != '+' && op != '-' && op != '.')
4710 break;
4711
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712 if ((op != '+' || rettv->v_type != VAR_LIST)
4713#ifdef FEAT_FLOAT
4714 && (op == '.' || rettv->v_type != VAR_FLOAT)
4715#endif
4716 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 {
4718 /* For "list + ...", an illegal use of the first operand as
4719 * a number cannot be determined before evaluating the 2nd
4720 * operand: if this is also a list, all is ok.
4721 * For "something . ...", "something - ..." or "non-list + ...",
4722 * we know that the first operand needs to be a string or number
4723 * without evaluating the 2nd operand. So check before to avoid
4724 * side effects after an error. */
4725 if (evaluate && get_tv_string_chk(rettv) == NULL)
4726 {
4727 clear_tv(rettv);
4728 return FAIL;
4729 }
4730 }
4731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 /*
4733 * Get the second variable.
4734 */
4735 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004736 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004738 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 return FAIL;
4740 }
4741
4742 if (evaluate)
4743 {
4744 /*
4745 * Compute the result.
4746 */
4747 if (op == '.')
4748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004749 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4750 s2 = get_tv_string_buf_chk(&var2, buf2);
4751 if (s2 == NULL) /* type error ? */
4752 {
4753 clear_tv(rettv);
4754 clear_tv(&var2);
4755 return FAIL;
4756 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004757 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 clear_tv(rettv);
4759 rettv->v_type = VAR_STRING;
4760 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004762 else if (op == '+' && rettv->v_type == VAR_LIST
4763 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004764 {
4765 /* concatenate Lists */
4766 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4767 &var3) == FAIL)
4768 {
4769 clear_tv(rettv);
4770 clear_tv(&var2);
4771 return FAIL;
4772 }
4773 clear_tv(rettv);
4774 *rettv = var3;
4775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 else
4777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778 int error = FALSE;
4779
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780#ifdef FEAT_FLOAT
4781 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783 f1 = rettv->vval.v_float;
4784 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 else
4787#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 n1 = get_tv_number_chk(rettv, &error);
4790 if (error)
4791 {
4792 /* This can only happen for "list + non-list". For
4793 * "non-list + ..." or "something - ...", we returned
4794 * before evaluating the 2nd operand. */
4795 clear_tv(rettv);
4796 return FAIL;
4797 }
4798#ifdef FEAT_FLOAT
4799 if (var2.v_type == VAR_FLOAT)
4800 f1 = n1;
4801#endif
4802 }
4803#ifdef FEAT_FLOAT
4804 if (var2.v_type == VAR_FLOAT)
4805 {
4806 f2 = var2.vval.v_float;
4807 n2 = 0;
4808 }
4809 else
4810#endif
4811 {
4812 n2 = get_tv_number_chk(&var2, &error);
4813 if (error)
4814 {
4815 clear_tv(rettv);
4816 clear_tv(&var2);
4817 return FAIL;
4818 }
4819#ifdef FEAT_FLOAT
4820 if (rettv->v_type == VAR_FLOAT)
4821 f2 = n2;
4822#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004823 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004824 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004825
4826#ifdef FEAT_FLOAT
4827 /* If there is a float on either side the result is a float. */
4828 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4829 {
4830 if (op == '+')
4831 f1 = f1 + f2;
4832 else
4833 f1 = f1 - f2;
4834 rettv->v_type = VAR_FLOAT;
4835 rettv->vval.v_float = f1;
4836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004838#endif
4839 {
4840 if (op == '+')
4841 n1 = n1 + n2;
4842 else
4843 n1 = n1 - n2;
4844 rettv->v_type = VAR_NUMBER;
4845 rettv->vval.v_number = n1;
4846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850 }
4851 return OK;
4852}
4853
4854/*
4855 * Handle fifth level expression:
4856 * * number multiplication
4857 * / number division
4858 * % number modulo
4859 *
4860 * "arg" must point to the first non-white of the expression.
4861 * "arg" is advanced to the next non-white after the recognized expression.
4862 *
4863 * Return OK or FAIL.
4864 */
4865 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004866eval6(
4867 char_u **arg,
4868 typval_T *rettv,
4869 int evaluate,
4870 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
Bram Moolenaar33570922005-01-25 22:26:29 +00004872 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 int op;
4874 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004875#ifdef FEAT_FLOAT
4876 int use_float = FALSE;
4877 float_T f1 = 0, f2;
4878#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 /*
4882 * Get the first variable.
4883 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004884 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 return FAIL;
4886
4887 /*
4888 * Repeat computing, until no '*', '/' or '%' is following.
4889 */
4890 for (;;)
4891 {
4892 op = **arg;
4893 if (op != '*' && op != '/' && op != '%')
4894 break;
4895
4896 if (evaluate)
4897 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898#ifdef FEAT_FLOAT
4899 if (rettv->v_type == VAR_FLOAT)
4900 {
4901 f1 = rettv->vval.v_float;
4902 use_float = TRUE;
4903 n1 = 0;
4904 }
4905 else
4906#endif
4907 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004908 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004909 if (error)
4910 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 else
4913 n1 = 0;
4914
4915 /*
4916 * Get the second variable.
4917 */
4918 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004919 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 return FAIL;
4921
4922 if (evaluate)
4923 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924#ifdef FEAT_FLOAT
4925 if (var2.v_type == VAR_FLOAT)
4926 {
4927 if (!use_float)
4928 {
4929 f1 = n1;
4930 use_float = TRUE;
4931 }
4932 f2 = var2.vval.v_float;
4933 n2 = 0;
4934 }
4935 else
4936#endif
4937 {
4938 n2 = get_tv_number_chk(&var2, &error);
4939 clear_tv(&var2);
4940 if (error)
4941 return FAIL;
4942#ifdef FEAT_FLOAT
4943 if (use_float)
4944 f2 = n2;
4945#endif
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
4948 /*
4949 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004950 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952#ifdef FEAT_FLOAT
4953 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 if (op == '*')
4956 f1 = f1 * f2;
4957 else if (op == '/')
4958 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004959# ifdef VMS
4960 /* VMS crashes on divide by zero, work around it */
4961 if (f2 == 0.0)
4962 {
4963 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004964 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004965 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004966 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004967 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004968 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004969 }
4970 else
4971 f1 = f1 / f2;
4972# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004973 /* We rely on the floating point library to handle divide
4974 * by zero to result in "inf" and not a crash. */
4975 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004976# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004980 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 return FAIL;
4982 }
4983 rettv->v_type = VAR_FLOAT;
4984 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004989 if (op == '*')
4990 n1 = n1 * n2;
4991 else if (op == '/')
4992 {
4993 if (n2 == 0) /* give an error message? */
4994 {
4995 if (n1 == 0)
4996 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4997 else if (n1 < 0)
4998 n1 = -0x7fffffffL;
4999 else
5000 n1 = 0x7fffffffL;
5001 }
5002 else
5003 n1 = n1 / n2;
5004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 {
5007 if (n2 == 0) /* give an error message? */
5008 n1 = 0;
5009 else
5010 n1 = n1 % n2;
5011 }
5012 rettv->v_type = VAR_NUMBER;
5013 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016 }
5017
5018 return OK;
5019}
5020
5021/*
5022 * Handle sixth level expression:
5023 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005024 * "string" string constant
5025 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 * &option-name option value
5027 * @r register contents
5028 * identifier variable value
5029 * function() function call
5030 * $VAR environment variable
5031 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005032 * [expr, expr] List
5033 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 *
5035 * Also handle:
5036 * ! in front logical NOT
5037 * - in front unary minus
5038 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 * trailing [] subscript in String or List
5040 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *
5042 * "arg" must point to the first non-white of the expression.
5043 * "arg" is advanced to the next non-white after the recognized expression.
5044 *
5045 * Return OK or FAIL.
5046 */
5047 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005048eval7(
5049 char_u **arg,
5050 typval_T *rettv,
5051 int evaluate,
5052 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 long n;
5055 int len;
5056 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 char_u *start_leader, *end_leader;
5058 int ret = OK;
5059 char_u *alias;
5060
5061 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005063 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
5067 /*
5068 * Skip '!' and '-' characters. They are handled later.
5069 */
5070 start_leader = *arg;
5071 while (**arg == '!' || **arg == '-' || **arg == '+')
5072 *arg = skipwhite(*arg + 1);
5073 end_leader = *arg;
5074
5075 switch (**arg)
5076 {
5077 /*
5078 * Number constant.
5079 */
5080 case '0':
5081 case '1':
5082 case '2':
5083 case '3':
5084 case '4':
5085 case '5':
5086 case '6':
5087 case '7':
5088 case '8':
5089 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005090 {
5091#ifdef FEAT_FLOAT
5092 char_u *p = skipdigits(*arg + 1);
5093 int get_float = FALSE;
5094
5095 /* We accept a float when the format matches
5096 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005097 * strict to avoid backwards compatibility problems.
5098 * Don't look for a float after the "." operator, so that
5099 * ":let vers = 1.2.3" doesn't fail. */
5100 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 get_float = TRUE;
5103 p = skipdigits(p + 2);
5104 if (*p == 'e' || *p == 'E')
5105 {
5106 ++p;
5107 if (*p == '-' || *p == '+')
5108 ++p;
5109 if (!vim_isdigit(*p))
5110 get_float = FALSE;
5111 else
5112 p = skipdigits(p + 1);
5113 }
5114 if (ASCII_ISALPHA(*p) || *p == '.')
5115 get_float = FALSE;
5116 }
5117 if (get_float)
5118 {
5119 float_T f;
5120
5121 *arg += string2float(*arg, &f);
5122 if (evaluate)
5123 {
5124 rettv->v_type = VAR_FLOAT;
5125 rettv->vval.v_float = f;
5126 }
5127 }
5128 else
5129#endif
5130 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005131 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005132 *arg += len;
5133 if (evaluate)
5134 {
5135 rettv->v_type = VAR_NUMBER;
5136 rettv->vval.v_number = n;
5137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142 /*
5143 * String constant: "string".
5144 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 break;
5147
5148 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152 break;
5153
5154 /*
5155 * List: [expr, expr]
5156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 break;
5159
5160 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 * Dictionary: {key: val, key: val}
5162 */
5163 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5164 break;
5165
5166 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005167 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005169 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 break;
5171
5172 /*
5173 * Environment variable: $VAR.
5174 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
5179 * Register contents: @r.
5180 */
5181 case '@': ++*arg;
5182 if (evaluate)
5183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005184 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005185 rettv->vval.v_string = get_reg_contents(**arg,
5186 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
5188 if (**arg != NUL)
5189 ++*arg;
5190 break;
5191
5192 /*
5193 * nested expression: (expression).
5194 */
5195 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (**arg == ')')
5198 ++*arg;
5199 else if (ret == OK)
5200 {
5201 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 ret = FAIL;
5204 }
5205 break;
5206
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 break;
5209 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210
5211 if (ret == NOTDONE)
5212 {
5213 /*
5214 * Must be a variable or function name.
5215 * Can also be a curly-braces kind of name: {expr}.
5216 */
5217 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005218 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 if (alias != NULL)
5220 s = alias;
5221
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005222 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 ret = FAIL;
5224 else
5225 {
5226 if (**arg == '(') /* recursive! */
5227 {
5228 /* If "s" is the name of a variable of type VAR_FUNC
5229 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005230 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231
5232 /* Invoke the function. */
5233 ret = get_func_tv(s, len, rettv, arg,
5234 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005235 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005236
5237 /* If evaluate is FALSE rettv->v_type was not set in
5238 * get_func_tv, but it's needed in handle_subscript() to parse
5239 * what follows. So set it here. */
5240 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5241 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005242 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005243 rettv->v_type = VAR_FUNC;
5244 }
5245
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 /* Stop the expression evaluation when immediately
5247 * aborting on error, or when an interrupt occurred or
5248 * an exception was thrown but not caught. */
5249 if (aborting())
5250 {
5251 if (ret == OK)
5252 clear_tv(rettv);
5253 ret = FAIL;
5254 }
5255 }
5256 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005257 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005258 else
5259 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005261 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 }
5263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 *arg = skipwhite(*arg);
5265
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005266 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5267 * expr(expr). */
5268 if (ret == OK)
5269 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 /*
5272 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5273 */
5274 if (ret == OK && evaluate && end_leader > start_leader)
5275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005276 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005277 int val = 0;
5278#ifdef FEAT_FLOAT
5279 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005281 if (rettv->v_type == VAR_FLOAT)
5282 f = rettv->vval.v_float;
5283 else
5284#endif
5285 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005286 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288 clear_tv(rettv);
5289 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 else
5292 {
5293 while (end_leader > start_leader)
5294 {
5295 --end_leader;
5296 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005297 {
5298#ifdef FEAT_FLOAT
5299 if (rettv->v_type == VAR_FLOAT)
5300 f = !f;
5301 else
5302#endif
5303 val = !val;
5304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005305 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005306 {
5307#ifdef FEAT_FLOAT
5308 if (rettv->v_type == VAR_FLOAT)
5309 f = -f;
5310 else
5311#endif
5312 val = -val;
5313 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005314 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005315#ifdef FEAT_FLOAT
5316 if (rettv->v_type == VAR_FLOAT)
5317 {
5318 clear_tv(rettv);
5319 rettv->vval.v_float = f;
5320 }
5321 else
5322#endif
5323 {
5324 clear_tv(rettv);
5325 rettv->v_type = VAR_NUMBER;
5326 rettv->vval.v_number = val;
5327 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330
5331 return ret;
5332}
5333
5334/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005335 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5336 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5338 */
5339 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005340eval_index(
5341 char_u **arg,
5342 typval_T *rettv,
5343 int evaluate,
5344 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345{
5346 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 long len = -1;
5350 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005352 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353
Bram Moolenaara03f2332016-02-06 18:09:59 +01005354 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005356 case VAR_FUNC:
5357 if (verbose)
5358 EMSG(_("E695: Cannot index a Funcref"));
5359 return FAIL;
5360 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005361#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 if (verbose)
5363 EMSG(_(e_float_as_string));
5364 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005365#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005366 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005367 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005368 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005369 if (verbose)
5370 EMSG(_("E909: Cannot index a special variable"));
5371 return FAIL;
5372 case VAR_UNKNOWN:
5373 if (evaluate)
5374 return FAIL;
5375 /* FALLTHROUGH */
5376
5377 case VAR_STRING:
5378 case VAR_NUMBER:
5379 case VAR_LIST:
5380 case VAR_DICT:
5381 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005382 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005384 init_tv(&var1);
5385 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388 /*
5389 * dict.name
5390 */
5391 key = *arg + 1;
5392 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5393 ;
5394 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 }
5398 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 /*
5401 * something[idx]
5402 *
5403 * Get the (first) variable from inside the [].
5404 */
5405 *arg = skipwhite(*arg + 1);
5406 if (**arg == ':')
5407 empty1 = TRUE;
5408 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5409 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005410 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5411 {
5412 /* not a number or string */
5413 clear_tv(&var1);
5414 return FAIL;
5415 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416
5417 /*
5418 * Get the second variable from inside the [:].
5419 */
5420 if (**arg == ':')
5421 {
5422 range = TRUE;
5423 *arg = skipwhite(*arg + 1);
5424 if (**arg == ']')
5425 empty2 = TRUE;
5426 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005428 if (!empty1)
5429 clear_tv(&var1);
5430 return FAIL;
5431 }
5432 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5433 {
5434 /* not a number or string */
5435 if (!empty1)
5436 clear_tv(&var1);
5437 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005438 return FAIL;
5439 }
5440 }
5441
5442 /* Check for the ']'. */
5443 if (**arg != ']')
5444 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005445 if (verbose)
5446 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 clear_tv(&var1);
5448 if (range)
5449 clear_tv(&var2);
5450 return FAIL;
5451 }
5452 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 }
5454
5455 if (evaluate)
5456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 n1 = 0;
5458 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 n1 = get_tv_number(&var1);
5461 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 }
5463 if (range)
5464 {
5465 if (empty2)
5466 n2 = -1;
5467 else
5468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 n2 = get_tv_number(&var2);
5470 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 }
5472 }
5473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005476 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005477 case VAR_FUNC:
5478 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005479 case VAR_SPECIAL:
5480 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005481 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005482 break; /* not evaluating, skipping over subscript */
5483
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 case VAR_NUMBER:
5485 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005486 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 len = (long)STRLEN(s);
5488 if (range)
5489 {
5490 /* The resulting variable is a substring. If the indexes
5491 * are out of range the result is empty. */
5492 if (n1 < 0)
5493 {
5494 n1 = len + n1;
5495 if (n1 < 0)
5496 n1 = 0;
5497 }
5498 if (n2 < 0)
5499 n2 = len + n2;
5500 else if (n2 >= len)
5501 n2 = len;
5502 if (n1 >= len || n2 < 0 || n1 > n2)
5503 s = NULL;
5504 else
5505 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5506 }
5507 else
5508 {
5509 /* The resulting variable is a string of a single
5510 * character. If the index is too big or negative the
5511 * result is empty. */
5512 if (n1 >= len || n1 < 0)
5513 s = NULL;
5514 else
5515 s = vim_strnsave(s + n1, 1);
5516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517 clear_tv(rettv);
5518 rettv->v_type = VAR_STRING;
5519 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 break;
5521
5522 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 if (n1 < 0)
5525 n1 = len + n1;
5526 if (!empty1 && (n1 < 0 || n1 >= len))
5527 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005528 /* For a range we allow invalid values and return an empty
5529 * list. A list index out of range is an error. */
5530 if (!range)
5531 {
5532 if (verbose)
5533 EMSGN(_(e_listidx), n1);
5534 return FAIL;
5535 }
5536 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 }
5538 if (range)
5539 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005540 list_T *l;
5541 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542
5543 if (n2 < 0)
5544 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005545 else if (n2 >= len)
5546 n2 = len - 1;
5547 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005548 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549 l = list_alloc();
5550 if (l == NULL)
5551 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 n1 <= n2; ++n1)
5554 {
5555 if (list_append_tv(l, &item->li_tv) == FAIL)
5556 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005557 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 return FAIL;
5559 }
5560 item = item->li_next;
5561 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 clear_tv(rettv);
5563 rettv->v_type = VAR_LIST;
5564 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005565 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 }
5567 else
5568 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005569 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 clear_tv(rettv);
5571 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 }
5573 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574
5575 case VAR_DICT:
5576 if (range)
5577 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005578 if (verbose)
5579 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580 if (len == -1)
5581 clear_tv(&var1);
5582 return FAIL;
5583 }
5584 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005585 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586
5587 if (len == -1)
5588 {
5589 key = get_tv_string(&var1);
5590 if (*key == NUL)
5591 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005592 if (verbose)
5593 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594 clear_tv(&var1);
5595 return FAIL;
5596 }
5597 }
5598
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005599 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005601 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005602 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 if (len == -1)
5604 clear_tv(&var1);
5605 if (item == NULL)
5606 return FAIL;
5607
5608 copy_tv(&item->di_tv, &var1);
5609 clear_tv(rettv);
5610 *rettv = var1;
5611 }
5612 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 }
5614 }
5615
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 return OK;
5617}
5618
5619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 * Get an option value.
5621 * "arg" points to the '&' or '+' before the option name.
5622 * "arg" is advanced to character after the option name.
5623 * Return OK or FAIL.
5624 */
5625 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005626get_option_tv(
5627 char_u **arg,
5628 typval_T *rettv, /* when NULL, only check if option exists */
5629 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631 char_u *option_end;
5632 long numval;
5633 char_u *stringval;
5634 int opt_type;
5635 int c;
5636 int working = (**arg == '+'); /* has("+option") */
5637 int ret = OK;
5638 int opt_flags;
5639
5640 /*
5641 * Isolate the option name and find its value.
5642 */
5643 option_end = find_option_end(arg, &opt_flags);
5644 if (option_end == NULL)
5645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 EMSG2(_("E112: Option name missing: %s"), *arg);
5648 return FAIL;
5649 }
5650
5651 if (!evaluate)
5652 {
5653 *arg = option_end;
5654 return OK;
5655 }
5656
5657 c = *option_end;
5658 *option_end = NUL;
5659 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 if (opt_type == -3) /* invalid name */
5663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 EMSG2(_("E113: Unknown option: %s"), *arg);
5666 ret = FAIL;
5667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 {
5670 if (opt_type == -2) /* hidden string option */
5671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 rettv->v_type = VAR_STRING;
5673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675 else if (opt_type == -1) /* hidden number option */
5676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 rettv->v_type = VAR_NUMBER;
5678 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680 else if (opt_type == 1) /* number option */
5681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005682 rettv->v_type = VAR_NUMBER;
5683 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 else /* string option */
5686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 rettv->v_type = VAR_STRING;
5688 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690 }
5691 else if (working && (opt_type == -2 || opt_type == -1))
5692 ret = FAIL;
5693
5694 *option_end = c; /* put back for error messages */
5695 *arg = option_end;
5696
5697 return ret;
5698}
5699
5700/*
5701 * Allocate a variable for a string constant.
5702 * Return OK or FAIL.
5703 */
5704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005705get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
5707 char_u *p;
5708 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 int extra = 0;
5710
5711 /*
5712 * Find the end of the string, skipping backslashed characters.
5713 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005714 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 {
5716 if (*p == '\\' && p[1] != NUL)
5717 {
5718 ++p;
5719 /* A "\<x>" form occupies at least 4 characters, and produces up
5720 * to 6 characters: reserve space for 2 extra */
5721 if (*p == '<')
5722 extra += 2;
5723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
5725
5726 if (*p != '"')
5727 {
5728 EMSG2(_("E114: Missing quote: %s"), *arg);
5729 return FAIL;
5730 }
5731
5732 /* If only parsing, set *arg and return here */
5733 if (!evaluate)
5734 {
5735 *arg = p + 1;
5736 return OK;
5737 }
5738
5739 /*
5740 * Copy the string into allocated memory, handling backslashed
5741 * characters.
5742 */
5743 name = alloc((unsigned)(p - *arg + extra));
5744 if (name == NULL)
5745 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 rettv->v_type = VAR_STRING;
5747 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 if (*p == '\\')
5752 {
5753 switch (*++p)
5754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 case 'b': *name++ = BS; ++p; break;
5756 case 'e': *name++ = ESC; ++p; break;
5757 case 'f': *name++ = FF; ++p; break;
5758 case 'n': *name++ = NL; ++p; break;
5759 case 'r': *name++ = CAR; ++p; break;
5760 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761
5762 case 'X': /* hex: "\x1", "\x12" */
5763 case 'x':
5764 case 'u': /* Unicode: "\u0023" */
5765 case 'U':
5766 if (vim_isxdigit(p[1]))
5767 {
5768 int n, nr;
5769 int c = toupper(*p);
5770
5771 if (c == 'X')
5772 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005773 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005775 else
5776 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 nr = 0;
5778 while (--n >= 0 && vim_isxdigit(p[1]))
5779 {
5780 ++p;
5781 nr = (nr << 4) + hex2nr(*p);
5782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784#ifdef FEAT_MBYTE
5785 /* For "\u" store the number according to
5786 * 'encoding'. */
5787 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 else
5790#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 break;
5794
5795 /* octal: "\1", "\12", "\123" */
5796 case '0':
5797 case '1':
5798 case '2':
5799 case '3':
5800 case '4':
5801 case '5':
5802 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 case '7': *name = *p++ - '0';
5804 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 *name = (*name << 3) + *p++ - '0';
5807 if (*p >= '0' && *p <= '7')
5808 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 break;
5812
5813 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 if (extra != 0)
5816 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819 }
5820 /* FALLTHROUGH */
5821
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 break;
5824 }
5825 }
5826 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 *arg = p + 1;
5832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 return OK;
5834}
5835
5836/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 * Return OK or FAIL.
5839 */
5840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005841get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
5843 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 int reduce = 0;
5846
5847 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 */
5850 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5851 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 break;
5856 ++reduce;
5857 ++p;
5858 }
5859 }
5860
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 return FAIL;
5865 }
5866
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 if (!evaluate)
5869 {
5870 *arg = p + 1;
5871 return OK;
5872 }
5873
5874 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 */
5877 str = alloc((unsigned)((p - *arg) - reduce));
5878 if (str == NULL)
5879 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 rettv->v_type = VAR_STRING;
5881 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 break;
5889 ++p;
5890 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 *arg = p + 1;
5895
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 return OK;
5897}
5898
5899/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 * Allocate a variable for a List and fill it from "*arg".
5901 * Return OK or FAIL.
5902 */
5903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005904get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905{
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 list_T *l = NULL;
5907 typval_T tv;
5908 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909
5910 if (evaluate)
5911 {
5912 l = list_alloc();
5913 if (l == NULL)
5914 return FAIL;
5915 }
5916
5917 *arg = skipwhite(*arg + 1);
5918 while (**arg != ']' && **arg != NUL)
5919 {
5920 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5921 goto failret;
5922 if (evaluate)
5923 {
5924 item = listitem_alloc();
5925 if (item != NULL)
5926 {
5927 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005928 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 list_append(l, item);
5930 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005931 else
5932 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 }
5934
5935 if (**arg == ']')
5936 break;
5937 if (**arg != ',')
5938 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 goto failret;
5941 }
5942 *arg = skipwhite(*arg + 1);
5943 }
5944
5945 if (**arg != ']')
5946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948failret:
5949 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005950 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 return FAIL;
5952 }
5953
5954 *arg = skipwhite(*arg + 1);
5955 if (evaluate)
5956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005957 rettv->v_type = VAR_LIST;
5958 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 ++l->lv_refcount;
5960 }
5961
5962 return OK;
5963}
5964
5965/*
5966 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005967 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005969 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005970list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005972 list_T *l;
5973
5974 l = (list_T *)alloc_clear(sizeof(list_T));
5975 if (l != NULL)
5976 {
5977 /* Prepend the list to the list of lists for garbage collection. */
5978 if (first_list != NULL)
5979 first_list->lv_used_prev = l;
5980 l->lv_used_prev = NULL;
5981 l->lv_used_next = first_list;
5982 first_list = l;
5983 }
5984 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985}
5986
5987/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005988 * Allocate an empty list for a return value.
5989 * Returns OK or FAIL.
5990 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005991 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005992rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005993{
5994 list_T *l = list_alloc();
5995
5996 if (l == NULL)
5997 return FAIL;
5998
5999 rettv->vval.v_list = l;
6000 rettv->v_type = VAR_LIST;
6001 ++l->lv_refcount;
6002 return OK;
6003}
6004
6005/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 * Unreference a list: decrement the reference count and free it when it
6007 * becomes zero.
6008 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006009 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006010list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006012 if (l != NULL && --l->lv_refcount <= 0)
6013 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014}
6015
6016/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006017 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 * Ignores the reference count.
6019 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006020 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006021list_free(
6022 list_T *l,
6023 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024{
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006027 /* Remove the list from the list of lists for garbage collection. */
6028 if (l->lv_used_prev == NULL)
6029 first_list = l->lv_used_next;
6030 else
6031 l->lv_used_prev->lv_used_next = l->lv_used_next;
6032 if (l->lv_used_next != NULL)
6033 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6034
Bram Moolenaard9fba312005-06-26 22:34:35 +00006035 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006037 /* Remove the item before deleting it. */
6038 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006039 if (recurse || (item->li_tv.v_type != VAR_LIST
6040 && item->li_tv.v_type != VAR_DICT))
6041 clear_tv(&item->li_tv);
6042 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 }
6044 vim_free(l);
6045}
6046
6047/*
6048 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006049 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006051 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006052listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053{
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055}
6056
6057/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006058 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006061listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006063 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 vim_free(item);
6065}
6066
6067/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006068 * Remove a list item from a List and free it. Also clears the value.
6069 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006071listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006072{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006073 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006074 listitem_free(item);
6075}
6076
6077/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 * Get the number of items in a list.
6079 */
6080 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006081list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 if (l == NULL)
6084 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006085 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086}
6087
6088/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006089 * Return TRUE when two lists have exactly the same values.
6090 */
6091 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006092list_equal(
6093 list_T *l1,
6094 list_T *l2,
6095 int ic, /* ignore case for strings */
6096 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097{
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006100 if (l1 == NULL || l2 == NULL)
6101 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006102 if (l1 == l2)
6103 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006104 if (list_len(l1) != list_len(l2))
6105 return FALSE;
6106
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107 for (item1 = l1->lv_first, item2 = l2->lv_first;
6108 item1 != NULL && item2 != NULL;
6109 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111 return FALSE;
6112 return item1 == NULL && item2 == NULL;
6113}
6114
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006115/*
6116 * Return the dictitem that an entry in a hashtable points to.
6117 */
6118 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006120{
6121 return HI2DI(hi);
6122}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006123
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006124/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006125 * Return TRUE when two dictionaries have exactly the same key/values.
6126 */
6127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006128dict_equal(
6129 dict_T *d1,
6130 dict_T *d2,
6131 int ic, /* ignore case for strings */
6132 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006133{
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 hashitem_T *hi;
6135 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006136 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006137
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006138 if (d1 == NULL || d2 == NULL)
6139 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006140 if (d1 == d2)
6141 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006142 if (dict_len(d1) != dict_len(d2))
6143 return FALSE;
6144
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006145 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006147 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006148 if (!HASHITEM_EMPTY(hi))
6149 {
6150 item2 = dict_find(d2, hi->hi_key, -1);
6151 if (item2 == NULL)
6152 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 return FALSE;
6155 --todo;
6156 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006157 }
6158 return TRUE;
6159}
6160
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006161static int tv_equal_recurse_limit;
6162
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006165 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006166 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167 */
6168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006169tv_equal(
6170 typval_T *tv1,
6171 typval_T *tv2,
6172 int ic, /* ignore case */
6173 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174{
6175 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006176 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006177 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006178 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006179
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006180 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006181 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006182
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006183 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 * recursiveness to a limit. We guess they are equal then.
6185 * A fixed limit has the problem of still taking an awful long time.
6186 * Reduce the limit every time running into it. That should work fine for
6187 * deeply linked structures that are not recursively linked and catch
6188 * recursiveness quickly. */
6189 if (!recursive)
6190 tv_equal_recurse_limit = 1000;
6191 if (recursive_cnt >= tv_equal_recurse_limit)
6192 {
6193 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006194 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006195 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006196
6197 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006198 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006200 ++recursive_cnt;
6201 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6202 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006203 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006204
6205 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 ++recursive_cnt;
6207 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6208 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006209 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210
6211 case VAR_FUNC:
6212 return (tv1->vval.v_string != NULL
6213 && tv2->vval.v_string != NULL
6214 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6215
6216 case VAR_NUMBER:
6217 return tv1->vval.v_number == tv2->vval.v_number;
6218
6219 case VAR_STRING:
6220 s1 = get_tv_string_buf(tv1, buf1);
6221 s2 = get_tv_string_buf(tv2, buf2);
6222 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006223
6224 case VAR_SPECIAL:
6225 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006226
6227 case VAR_FLOAT:
6228#ifdef FEAT_FLOAT
6229 return tv1->vval.v_float == tv2->vval.v_float;
6230#endif
6231 case VAR_JOB:
6232#ifdef FEAT_JOB
6233 return tv1->vval.v_job == tv2->vval.v_job;
6234#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006235 case VAR_CHANNEL:
6236#ifdef FEAT_CHANNEL
6237 return tv1->vval.v_channel == tv2->vval.v_channel;
6238#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006239 case VAR_UNKNOWN:
6240 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006241 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242
Bram Moolenaara03f2332016-02-06 18:09:59 +01006243 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6244 * does not equal anything, not even itself. */
6245 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006246}
6247
6248/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006249 * Locate item with index "n" in list "l" and return it.
6250 * A negative index is counted from the end; -1 is the last item.
6251 * Returns NULL when "n" is out of range.
6252 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006253 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006254list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006255{
Bram Moolenaar33570922005-01-25 22:26:29 +00006256 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257 long idx;
6258
6259 if (l == NULL)
6260 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006261
6262 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006264 n = l->lv_len + n;
6265
6266 /* Check for index out of range. */
6267 if (n < 0 || n >= l->lv_len)
6268 return NULL;
6269
6270 /* When there is a cached index may start search from there. */
6271 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006273 if (n < l->lv_idx / 2)
6274 {
6275 /* closest to the start of the list */
6276 item = l->lv_first;
6277 idx = 0;
6278 }
6279 else if (n > (l->lv_idx + l->lv_len) / 2)
6280 {
6281 /* closest to the end of the list */
6282 item = l->lv_last;
6283 idx = l->lv_len - 1;
6284 }
6285 else
6286 {
6287 /* closest to the cached index */
6288 item = l->lv_idx_item;
6289 idx = l->lv_idx;
6290 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291 }
6292 else
6293 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006294 if (n < l->lv_len / 2)
6295 {
6296 /* closest to the start of the list */
6297 item = l->lv_first;
6298 idx = 0;
6299 }
6300 else
6301 {
6302 /* closest to the end of the list */
6303 item = l->lv_last;
6304 idx = l->lv_len - 1;
6305 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006306 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006307
6308 while (n > idx)
6309 {
6310 /* search forward */
6311 item = item->li_next;
6312 ++idx;
6313 }
6314 while (n < idx)
6315 {
6316 /* search backward */
6317 item = item->li_prev;
6318 --idx;
6319 }
6320
6321 /* cache the used index */
6322 l->lv_idx = idx;
6323 l->lv_idx_item = item;
6324
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325 return item;
6326}
6327
6328/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006329 * Get list item "l[idx]" as a number.
6330 */
6331 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006332list_find_nr(
6333 list_T *l,
6334 long idx,
6335 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006336{
6337 listitem_T *li;
6338
6339 li = list_find(l, idx);
6340 if (li == NULL)
6341 {
6342 if (errorp != NULL)
6343 *errorp = TRUE;
6344 return -1L;
6345 }
6346 return get_tv_number_chk(&li->li_tv, errorp);
6347}
6348
6349/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006350 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6351 */
6352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006353list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006354{
6355 listitem_T *li;
6356
6357 li = list_find(l, idx - 1);
6358 if (li == NULL)
6359 {
6360 EMSGN(_(e_listidx), idx);
6361 return NULL;
6362 }
6363 return get_tv_string(&li->li_tv);
6364}
6365
6366/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006367 * Locate "item" list "l" and return its index.
6368 * Returns -1 when "item" is not in the list.
6369 */
6370 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006371list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006372{
6373 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375
6376 if (l == NULL)
6377 return -1;
6378 idx = 0;
6379 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6380 ++idx;
6381 if (li == NULL)
6382 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006383 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006384}
6385
6386/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387 * Append item "item" to the end of list "l".
6388 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006390list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391{
6392 if (l->lv_last == NULL)
6393 {
6394 /* empty list */
6395 l->lv_first = item;
6396 l->lv_last = item;
6397 item->li_prev = NULL;
6398 }
6399 else
6400 {
6401 l->lv_last->li_next = item;
6402 item->li_prev = l->lv_last;
6403 l->lv_last = item;
6404 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006405 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006406 item->li_next = NULL;
6407}
6408
6409/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006410 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006411 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006414list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006416 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417
Bram Moolenaar05159a02005-02-26 23:04:13 +00006418 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006420 copy_tv(tv, &li->li_tv);
6421 list_append(l, li);
6422 return OK;
6423}
6424
6425/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006426 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006427 * Return FAIL when out of memory.
6428 */
6429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006430list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006431{
6432 listitem_T *li = listitem_alloc();
6433
6434 if (li == NULL)
6435 return FAIL;
6436 li->li_tv.v_type = VAR_DICT;
6437 li->li_tv.v_lock = 0;
6438 li->li_tv.vval.v_dict = dict;
6439 list_append(list, li);
6440 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441 return OK;
6442}
6443
6444/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006445 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006446 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006447 * Returns FAIL when out of memory.
6448 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006450list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451{
6452 listitem_T *li = listitem_alloc();
6453
6454 if (li == NULL)
6455 return FAIL;
6456 list_append(l, li);
6457 li->li_tv.v_type = VAR_STRING;
6458 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006459 if (str == NULL)
6460 li->li_tv.vval.v_string = NULL;
6461 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006462 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463 return FAIL;
6464 return OK;
6465}
6466
6467/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006468 * Append "n" to list "l".
6469 * Returns FAIL when out of memory.
6470 */
6471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006472list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006473{
6474 listitem_T *li;
6475
6476 li = listitem_alloc();
6477 if (li == NULL)
6478 return FAIL;
6479 li->li_tv.v_type = VAR_NUMBER;
6480 li->li_tv.v_lock = 0;
6481 li->li_tv.vval.v_number = n;
6482 list_append(l, li);
6483 return OK;
6484}
6485
6486/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 * If "item" is NULL append at the end.
6489 * Return FAIL when out of memory.
6490 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006491 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006492list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493{
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495
6496 if (ni == NULL)
6497 return FAIL;
6498 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006499 list_insert(l, ni, item);
6500 return OK;
6501}
6502
6503 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006504list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006505{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506 if (item == NULL)
6507 /* Append new item at end of list. */
6508 list_append(l, ni);
6509 else
6510 {
6511 /* Insert new item before existing item. */
6512 ni->li_prev = item->li_prev;
6513 ni->li_next = item;
6514 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006515 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006517 ++l->lv_idx;
6518 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006520 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006521 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006522 l->lv_idx_item = NULL;
6523 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527}
6528
6529/*
6530 * Extend "l1" with "l2".
6531 * If "bef" is NULL append at the end, otherwise insert before this item.
6532 * Returns FAIL when out of memory.
6533 */
6534 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006535list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536{
Bram Moolenaar33570922005-01-25 22:26:29 +00006537 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006538 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006540 /* We also quit the loop when we have inserted the original item count of
6541 * the list, avoid a hang when we extend a list with itself. */
6542 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6544 return FAIL;
6545 return OK;
6546}
6547
6548/*
6549 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6550 * Return FAIL when out of memory.
6551 */
6552 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006553list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554{
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006557 if (l1 == NULL || l2 == NULL)
6558 return FAIL;
6559
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006561 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 if (l == NULL)
6563 return FAIL;
6564 tv->v_type = VAR_LIST;
6565 tv->vval.v_list = l;
6566
6567 /* append all items from the second list */
6568 return list_extend(l, l2, NULL);
6569}
6570
6571/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006572 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006574 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 * Returns NULL when out of memory.
6576 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006578list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579{
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 list_T *copy;
6581 listitem_T *item;
6582 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583
6584 if (orig == NULL)
6585 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586
6587 copy = list_alloc();
6588 if (copy != NULL)
6589 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590 if (copyID != 0)
6591 {
6592 /* Do this before adding the items, because one of the items may
6593 * refer back to this list. */
6594 orig->lv_copyID = copyID;
6595 orig->lv_copylist = copy;
6596 }
6597 for (item = orig->lv_first; item != NULL && !got_int;
6598 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 {
6600 ni = listitem_alloc();
6601 if (ni == NULL)
6602 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006603 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604 {
6605 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6606 {
6607 vim_free(ni);
6608 break;
6609 }
6610 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006612 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613 list_append(copy, ni);
6614 }
6615 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006616 if (item != NULL)
6617 {
6618 list_unref(copy);
6619 copy = NULL;
6620 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621 }
6622
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 return copy;
6624}
6625
6626/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006627 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006628 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006629 * This used to be called list_remove, but that conflicts with a Sun header
6630 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006632 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006633vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634{
Bram Moolenaar33570922005-01-25 22:26:29 +00006635 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006637 /* notify watchers */
6638 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006640 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641 list_fix_watch(l, ip);
6642 if (ip == item2)
6643 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006645
6646 if (item2->li_next == NULL)
6647 l->lv_last = item->li_prev;
6648 else
6649 item2->li_next->li_prev = item->li_prev;
6650 if (item->li_prev == NULL)
6651 l->lv_first = item2->li_next;
6652 else
6653 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006654 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006655}
6656
6657/*
6658 * Return an allocated string with the string representation of a list.
6659 * May return NULL.
6660 */
6661 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006662list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663{
6664 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006665
6666 if (tv->vval.v_list == NULL)
6667 return NULL;
6668 ga_init2(&ga, (int)sizeof(char), 80);
6669 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006670 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006671 {
6672 vim_free(ga.ga_data);
6673 return NULL;
6674 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675 ga_append(&ga, ']');
6676 ga_append(&ga, NUL);
6677 return (char_u *)ga.ga_data;
6678}
6679
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006680typedef struct join_S {
6681 char_u *s;
6682 char_u *tofree;
6683} join_T;
6684
6685 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006686list_join_inner(
6687 garray_T *gap, /* to store the result in */
6688 list_T *l,
6689 char_u *sep,
6690 int echo_style,
6691 int copyID,
6692 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006693{
6694 int i;
6695 join_T *p;
6696 int len;
6697 int sumlen = 0;
6698 int first = TRUE;
6699 char_u *tofree;
6700 char_u numbuf[NUMBUFLEN];
6701 listitem_T *item;
6702 char_u *s;
6703
6704 /* Stringify each item in the list. */
6705 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6706 {
6707 if (echo_style)
6708 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6709 else
6710 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6711 if (s == NULL)
6712 return FAIL;
6713
6714 len = (int)STRLEN(s);
6715 sumlen += len;
6716
Bram Moolenaarcde88542015-08-11 19:14:00 +02006717 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6719 if (tofree != NULL || s != numbuf)
6720 {
6721 p->s = s;
6722 p->tofree = tofree;
6723 }
6724 else
6725 {
6726 p->s = vim_strnsave(s, len);
6727 p->tofree = p->s;
6728 }
6729
6730 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006731 if (did_echo_string_emsg) /* recursion error, bail out */
6732 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006733 }
6734
6735 /* Allocate result buffer with its total size, avoid re-allocation and
6736 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6737 if (join_gap->ga_len >= 2)
6738 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6739 if (ga_grow(gap, sumlen + 2) == FAIL)
6740 return FAIL;
6741
6742 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6743 {
6744 if (first)
6745 first = FALSE;
6746 else
6747 ga_concat(gap, sep);
6748 p = ((join_T *)join_gap->ga_data) + i;
6749
6750 if (p->s != NULL)
6751 ga_concat(gap, p->s);
6752 line_breakcheck();
6753 }
6754
6755 return OK;
6756}
6757
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006758/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006759 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006760 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006761 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006764list_join(
6765 garray_T *gap,
6766 list_T *l,
6767 char_u *sep,
6768 int echo_style,
6769 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006770{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006771 garray_T join_ga;
6772 int retval;
6773 join_T *p;
6774 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006775
Bram Moolenaard39a7512015-04-16 22:51:22 +02006776 if (l->lv_len < 1)
6777 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006778 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6779 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6780
6781 /* Dispose each item in join_ga. */
6782 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006783 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006784 p = (join_T *)join_ga.ga_data;
6785 for (i = 0; i < join_ga.ga_len; ++i)
6786 {
6787 vim_free(p->tofree);
6788 ++p;
6789 }
6790 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006791 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006792
6793 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006794}
6795
6796/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006797 * Return the next (unique) copy ID.
6798 * Used for serializing nested structures.
6799 */
6800 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006801get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006802{
6803 current_copyID += COPYID_INC;
6804 return current_copyID;
6805}
6806
6807/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006808 * Garbage collection for lists and dictionaries.
6809 *
6810 * We use reference counts to be able to free most items right away when they
6811 * are no longer used. But for composite items it's possible that it becomes
6812 * unused while the reference count is > 0: When there is a recursive
6813 * reference. Example:
6814 * :let l = [1, 2, 3]
6815 * :let d = {9: l}
6816 * :let l[1] = d
6817 *
6818 * Since this is quite unusual we handle this with garbage collection: every
6819 * once in a while find out which lists and dicts are not referenced from any
6820 * variable.
6821 *
6822 * Here is a good reference text about garbage collection (refers to Python
6823 * but it applies to all reference-counting mechanisms):
6824 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006825 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006826
6827/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006828 * Do garbage collection for lists and dicts.
6829 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006830 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006832garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006834 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006835 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006836 buf_T *buf;
6837 win_T *wp;
6838 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006839 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006840 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006841 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006842#ifdef FEAT_WINDOWS
6843 tabpage_T *tp;
6844#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006846 /* Only do this once. */
6847 want_garbage_collect = FALSE;
6848 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006849 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006850
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006851 /* We advance by two because we add one for items referenced through
6852 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006853 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 /*
6856 * 1. Go through all accessible variables and mark all lists and dicts
6857 * with copyID.
6858 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006859
6860 /* Don't free variables in the previous_funccal list unless they are only
6861 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006862 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6864 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006865 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6866 NULL);
6867 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6868 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 }
6870
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006871 /* script-local variables */
6872 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006873 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874
6875 /* buffer-local variables */
6876 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6878 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879
6880 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006881 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006882 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6883 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006884#ifdef FEAT_AUTOCMD
6885 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006886 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6887 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006888#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006890#ifdef FEAT_WINDOWS
6891 /* tabpage-local variables */
6892 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6894 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006895#endif
6896
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899
6900 /* function-local variables */
6901 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6902 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6904 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905 }
6906
Bram Moolenaard812df62008-11-09 12:46:09 +00006907 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006909
Bram Moolenaar1dced572012-04-05 16:54:08 +02006910#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006911 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006912#endif
6913
Bram Moolenaardb913952012-06-29 12:54:53 +02006914#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006916#endif
6917
6918#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006920#endif
6921
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006922#ifdef FEAT_CHANNEL
6923 abort = abort || set_ref_in_channel(copyID);
6924#endif
6925
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006927 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 /*
6929 * 2. Free lists and dictionaries that are not referenced.
6930 */
6931 did_free = free_unref_items(copyID);
6932
6933 /*
6934 * 3. Check if any funccal can be freed now.
6935 */
6936 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006937 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 if (can_free_funccal(*pfc, copyID))
6939 {
6940 fc = *pfc;
6941 *pfc = fc->caller;
6942 free_funccal(fc, TRUE);
6943 did_free = TRUE;
6944 did_free_funccal = TRUE;
6945 }
6946 else
6947 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 if (did_free_funccal)
6950 /* When a funccal was freed some more items might be garbage
6951 * collected, so run again. */
6952 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006953 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 else if (p_verbose > 0)
6955 {
6956 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6957 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006958
6959 return did_free;
6960}
6961
6962/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006963 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 */
6965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006966free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006968 dict_T *dd, *dd_next;
6969 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970 int did_free = FALSE;
6971
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 */
6975 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006976 {
6977 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006980 /* Free the Dictionary and ordinary items it contains, but don't
6981 * recurse into Lists and Dictionaries, they will be in the list
6982 * of dicts or list of lists. */
6983 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006986 dd = dd_next;
6987 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988
6989 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 * Go through the list of lists and free items without the copyID.
6991 * But don't free a list that has a watcher (used in a for loop), these
6992 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 */
6994 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006995 {
6996 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6998 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007000 /* Free the List and ordinary items it contains, but don't recurse
7001 * into Lists and Dictionaries, they will be in the list of dicts
7002 * or list of lists. */
7003 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007006 ll = ll_next;
7007 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007008
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 return did_free;
7010}
7011
7012/*
7013 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007014 * "list_stack" is used to add lists to be marked. Can be NULL.
7015 *
7016 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007019set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020{
7021 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007022 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 hashtab_T *cur_ht;
7025 ht_stack_T *ht_stack = NULL;
7026 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 cur_ht = ht;
7029 for (;;)
7030 {
7031 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 /* Mark each item in the hashtab. If the item contains a hashtab
7034 * it is added to ht_stack, if it contains a list it is added to
7035 * list_stack. */
7036 todo = (int)cur_ht->ht_used;
7037 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7038 if (!HASHITEM_EMPTY(hi))
7039 {
7040 --todo;
7041 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7042 &ht_stack, list_stack);
7043 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045
7046 if (ht_stack == NULL)
7047 break;
7048
7049 /* take an item from the stack */
7050 cur_ht = ht_stack->ht;
7051 tempitem = ht_stack;
7052 ht_stack = ht_stack->prev;
7053 free(tempitem);
7054 }
7055
7056 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057}
7058
7059/*
7060 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7062 *
7063 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007066set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 listitem_T *li;
7069 int abort = FALSE;
7070 list_T *cur_l;
7071 list_stack_T *list_stack = NULL;
7072 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 cur_l = l;
7075 for (;;)
7076 {
7077 if (!abort)
7078 /* Mark each item in the list. If the item contains a hashtab
7079 * it is added to ht_stack, if it contains a list it is added to
7080 * list_stack. */
7081 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7082 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7083 ht_stack, &list_stack);
7084 if (list_stack == NULL)
7085 break;
7086
7087 /* take an item from the stack */
7088 cur_l = list_stack->list;
7089 tempitem = list_stack;
7090 list_stack = list_stack->prev;
7091 free(tempitem);
7092 }
7093
7094 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095}
7096
7097/*
7098 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007099 * "list_stack" is used to add lists to be marked. Can be NULL.
7100 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7101 *
7102 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007105set_ref_in_item(
7106 typval_T *tv,
7107 int copyID,
7108 ht_stack_T **ht_stack,
7109 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007110{
7111 dict_T *dd;
7112 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007114
Bram Moolenaara03f2332016-02-06 18:09:59 +01007115 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007116 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007117 dd = tv->vval.v_dict;
7118 if (dd != NULL && dd->dv_copyID != copyID)
7119 {
7120 /* Didn't see this dict yet. */
7121 dd->dv_copyID = copyID;
7122 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007123 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007124 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7125 }
7126 else
7127 {
7128 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7129 if (newitem == NULL)
7130 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 else
7132 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007133 newitem->ht = &dd->dv_hashtab;
7134 newitem->prev = *ht_stack;
7135 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007136 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007137 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007138 }
7139 }
7140 else if (tv->v_type == VAR_LIST)
7141 {
7142 ll = tv->vval.v_list;
7143 if (ll != NULL && ll->lv_copyID != copyID)
7144 {
7145 /* Didn't see this list yet. */
7146 ll->lv_copyID = copyID;
7147 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007148 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007149 abort = set_ref_in_list(ll, copyID, ht_stack);
7150 }
7151 else
7152 {
7153 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007154 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007155 if (newitem == NULL)
7156 abort = TRUE;
7157 else
7158 {
7159 newitem->list = ll;
7160 newitem->prev = *list_stack;
7161 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007162 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007163 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007164 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007165 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007167}
7168
7169/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170 * Allocate an empty header for a dictionary.
7171 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007172 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007173dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174{
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007176
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007178 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007179 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007180 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007181 if (first_dict != NULL)
7182 first_dict->dv_used_prev = d;
7183 d->dv_used_next = first_dict;
7184 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007185 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007186
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007188 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007189 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007190 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007191 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007192 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007194}
7195
7196/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007197 * Allocate an empty dict for a return value.
7198 * Returns OK or FAIL.
7199 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007200 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007201rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007202{
7203 dict_T *d = dict_alloc();
7204
7205 if (d == NULL)
7206 return FAIL;
7207
7208 rettv->vval.v_dict = d;
7209 rettv->v_type = VAR_DICT;
7210 ++d->dv_refcount;
7211 return OK;
7212}
7213
7214
7215/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216 * Unreference a Dictionary: decrement the reference count and free it when it
7217 * becomes zero.
7218 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007220dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007222 if (d != NULL && --d->dv_refcount <= 0)
7223 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224}
7225
7226/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007227 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228 * Ignores the reference count.
7229 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007231dict_free(
7232 dict_T *d,
7233 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007235 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007236 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007237 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007239 /* Remove the dict from the list of dicts for garbage collection. */
7240 if (d->dv_used_prev == NULL)
7241 first_dict = d->dv_used_next;
7242 else
7243 d->dv_used_prev->dv_used_next = d->dv_used_next;
7244 if (d->dv_used_next != NULL)
7245 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7246
7247 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007248 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007249 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007252 if (!HASHITEM_EMPTY(hi))
7253 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007254 /* Remove the item before deleting it, just in case there is
7255 * something recursive causing trouble. */
7256 di = HI2DI(hi);
7257 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007258 if (recurse || (di->di_tv.v_type != VAR_LIST
7259 && di->di_tv.v_type != VAR_DICT))
7260 clear_tv(&di->di_tv);
7261 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007262 --todo;
7263 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007265 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 vim_free(d);
7267}
7268
7269/*
7270 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 * The "key" is copied to the new item.
7272 * Note that the value of the item "di_tv" still needs to be initialized!
7273 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007275 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007276dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277{
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007280 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007281 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007282 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007284 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007285 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287}
7288
7289/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290 * Make a copy of a Dictionary item.
7291 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007292 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007293dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294{
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007297 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7298 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 if (di != NULL)
7300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007302 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 copy_tv(&org->di_tv, &di->di_tv);
7304 }
7305 return di;
7306}
7307
7308/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 * Remove item "item" from Dictionary "dict" and free it.
7310 */
7311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007312dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313{
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 if (HASHITEM_EMPTY(hi))
7318 EMSG2(_(e_intern2), "dictitem_remove()");
7319 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 dictitem_free(item);
7322}
7323
7324/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007325 * Free a dict item. Also clears the value.
7326 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007328dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007331 if (item->di_flags & DI_FLAGS_ALLOC)
7332 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333}
7334
7335/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7337 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007338 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 * Returns NULL when out of memory.
7340 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007342dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343{
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 dict_T *copy;
7345 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007346 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348
7349 if (orig == NULL)
7350 return NULL;
7351
7352 copy = dict_alloc();
7353 if (copy != NULL)
7354 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007355 if (copyID != 0)
7356 {
7357 orig->dv_copyID = copyID;
7358 orig->dv_copydict = copy;
7359 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007360 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007364 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 --todo;
7366
7367 di = dictitem_alloc(hi->hi_key);
7368 if (di == NULL)
7369 break;
7370 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007371 {
7372 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7373 copyID) == FAIL)
7374 {
7375 vim_free(di);
7376 break;
7377 }
7378 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007379 else
7380 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7381 if (dict_add(copy, di) == FAIL)
7382 {
7383 dictitem_free(di);
7384 break;
7385 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007388
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007390 if (todo > 0)
7391 {
7392 dict_unref(copy);
7393 copy = NULL;
7394 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395 }
7396
7397 return copy;
7398}
7399
7400/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007402 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007403 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007404 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007405dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406{
Bram Moolenaar33570922005-01-25 22:26:29 +00007407 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408}
7409
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007411 * Add a number or string entry to dictionary "d".
7412 * When "str" is NULL use number "nr", otherwise use "str".
7413 * Returns FAIL when out of memory and when key already exists.
7414 */
7415 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007416dict_add_nr_str(
7417 dict_T *d,
7418 char *key,
7419 long nr,
7420 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007421{
7422 dictitem_T *item;
7423
7424 item = dictitem_alloc((char_u *)key);
7425 if (item == NULL)
7426 return FAIL;
7427 item->di_tv.v_lock = 0;
7428 if (str == NULL)
7429 {
7430 item->di_tv.v_type = VAR_NUMBER;
7431 item->di_tv.vval.v_number = nr;
7432 }
7433 else
7434 {
7435 item->di_tv.v_type = VAR_STRING;
7436 item->di_tv.vval.v_string = vim_strsave(str);
7437 }
7438 if (dict_add(d, item) == FAIL)
7439 {
7440 dictitem_free(item);
7441 return FAIL;
7442 }
7443 return OK;
7444}
7445
7446/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007447 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007448 * Returns FAIL when out of memory and when key already exists.
7449 */
7450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007451dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007452{
7453 dictitem_T *item;
7454
7455 item = dictitem_alloc((char_u *)key);
7456 if (item == NULL)
7457 return FAIL;
7458 item->di_tv.v_lock = 0;
7459 item->di_tv.v_type = VAR_LIST;
7460 item->di_tv.vval.v_list = list;
7461 if (dict_add(d, item) == FAIL)
7462 {
7463 dictitem_free(item);
7464 return FAIL;
7465 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007466 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007467 return OK;
7468}
7469
7470/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007471 * Get the number of items in a Dictionary.
7472 */
7473 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007474dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476 if (d == NULL)
7477 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007478 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007479}
7480
7481/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 * Find item "key[len]" in Dictionary "d".
7483 * If "len" is negative use strlen(key).
7484 * Returns NULL when not found.
7485 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007486 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007487dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007489#define AKEYLEN 200
7490 char_u buf[AKEYLEN];
7491 char_u *akey;
7492 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495 if (len < 0)
7496 akey = key;
7497 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007498 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007499 tofree = akey = vim_strnsave(key, len);
7500 if (akey == NULL)
7501 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007502 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 else
7504 {
7505 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007506 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007507 akey = buf;
7508 }
7509
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 vim_free(tofree);
7512 if (HASHITEM_EMPTY(hi))
7513 return NULL;
7514 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515}
7516
7517/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007518 * Get a string item from a dictionary.
7519 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007520 * Returns NULL if the entry doesn't exist or out of memory.
7521 */
7522 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007523get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007524{
7525 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007526 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007527
7528 di = dict_find(d, key, -1);
7529 if (di == NULL)
7530 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007531 s = get_tv_string(&di->di_tv);
7532 if (save && s != NULL)
7533 s = vim_strsave(s);
7534 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007535}
7536
7537/*
7538 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007539 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007540 */
7541 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007542get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007543{
7544 dictitem_T *di;
7545
7546 di = dict_find(d, key, -1);
7547 if (di == NULL)
7548 return 0;
7549 return get_tv_number(&di->di_tv);
7550}
7551
7552/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553 * Return an allocated string with the string representation of a Dictionary.
7554 * May return NULL.
7555 */
7556 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007557dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558{
7559 garray_T ga;
7560 int first = TRUE;
7561 char_u *tofree;
7562 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007568 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569 return NULL;
7570 ga_init2(&ga, (int)sizeof(char), 80);
7571 ga_append(&ga, '{');
7572
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007573 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007574 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007576 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578 --todo;
7579
7580 if (first)
7581 first = FALSE;
7582 else
7583 ga_concat(&ga, (char_u *)", ");
7584
7585 tofree = string_quote(hi->hi_key, FALSE);
7586 if (tofree != NULL)
7587 {
7588 ga_concat(&ga, tofree);
7589 vim_free(tofree);
7590 }
7591 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007592 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007593 if (s != NULL)
7594 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007596 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007597 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007598 line_breakcheck();
7599
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007602 if (todo > 0)
7603 {
7604 vim_free(ga.ga_data);
7605 return NULL;
7606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607
7608 ga_append(&ga, '}');
7609 ga_append(&ga, NUL);
7610 return (char_u *)ga.ga_data;
7611}
7612
7613/*
7614 * Allocate a variable for a Dictionary and fill it from "*arg".
7615 * Return OK or FAIL. Returns NOTDONE for {expr}.
7616 */
7617 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007618get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619{
Bram Moolenaar33570922005-01-25 22:26:29 +00007620 dict_T *d = NULL;
7621 typval_T tvkey;
7622 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007623 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007626 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627
7628 /*
7629 * First check if it's not a curly-braces thing: {expr}.
7630 * Must do this without evaluating, otherwise a function may be called
7631 * twice. Unfortunately this means we need to call eval1() twice for the
7632 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007635 if (*start != '}')
7636 {
7637 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7638 return FAIL;
7639 if (*start == '}')
7640 return NOTDONE;
7641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642
7643 if (evaluate)
7644 {
7645 d = dict_alloc();
7646 if (d == NULL)
7647 return FAIL;
7648 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007649 tvkey.v_type = VAR_UNKNOWN;
7650 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651
7652 *arg = skipwhite(*arg + 1);
7653 while (**arg != '}' && **arg != NUL)
7654 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 goto failret;
7657 if (**arg != ':')
7658 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007659 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007660 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 goto failret;
7662 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007663 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007665 key = get_tv_string_buf_chk(&tvkey, buf);
7666 if (key == NULL || *key == NUL)
7667 {
7668 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7669 if (key != NULL)
7670 EMSG(_(e_emptykey));
7671 clear_tv(&tvkey);
7672 goto failret;
7673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675
7676 *arg = skipwhite(*arg + 1);
7677 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7678 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007679 if (evaluate)
7680 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 goto failret;
7682 }
7683 if (evaluate)
7684 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007685 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 if (item != NULL)
7687 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007688 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007689 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 clear_tv(&tv);
7691 goto failret;
7692 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007693 item = dictitem_alloc(key);
7694 clear_tv(&tvkey);
7695 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007698 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 if (dict_add(d, item) == FAIL)
7700 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701 }
7702 }
7703
7704 if (**arg == '}')
7705 break;
7706 if (**arg != ',')
7707 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007708 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709 goto failret;
7710 }
7711 *arg = skipwhite(*arg + 1);
7712 }
7713
7714 if (**arg != '}')
7715 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007716 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717failret:
7718 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007719 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 return FAIL;
7721 }
7722
7723 *arg = skipwhite(*arg + 1);
7724 if (evaluate)
7725 {
7726 rettv->v_type = VAR_DICT;
7727 rettv->vval.v_dict = d;
7728 ++d->dv_refcount;
7729 }
7730
7731 return OK;
7732}
7733
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007734#if defined(FEAT_CHANNEL) || defined(PROTO)
7735/*
7736 * Decrement the reference count on "channel" and free it when it goes down to
7737 * zero.
7738 * Returns TRUE when the channel was freed.
7739 */
7740 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007741channel_unref(channel_T *channel)
7742{
7743 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007744 {
Bram Moolenaar77073442016-02-13 23:23:53 +01007745 channel_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007746 return TRUE;
7747 }
7748 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007749}
7750#endif
7751
Bram Moolenaar835dc632016-02-07 14:27:38 +01007752#ifdef FEAT_JOB
7753 static void
7754job_free(job_T *job)
7755{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007756# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007757 if (job->jv_channel != NULL)
7758 {
7759 /* The channel doesn't count as a references for the job, we need to
7760 * NULL the reference when the job is freed. */
7761 job->jv_channel->ch_job = NULL;
7762 channel_unref(job->jv_channel);
7763 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007764# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007765 mch_clear_job(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007766 vim_free(job);
7767}
7768
7769 static void
7770job_unref(job_T *job)
7771{
7772 if (job != NULL && --job->jv_refcount <= 0)
7773 job_free(job);
7774}
7775
7776/*
7777 * Allocate a job. Sets the refcount to one.
7778 */
7779 static job_T *
7780job_alloc(void)
7781{
7782 job_T *job;
7783
7784 job = (job_T *)alloc_clear(sizeof(job_T));
7785 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007786 job->jv_refcount = 1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007787 return job;
7788}
7789
7790#endif
7791
Bram Moolenaar17a13432016-01-24 14:22:10 +01007792 static char *
7793get_var_special_name(int nr)
7794{
7795 switch (nr)
7796 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007797 case VVAL_FALSE: return "v:false";
7798 case VVAL_TRUE: return "v:true";
7799 case VVAL_NONE: return "v:none";
7800 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007801 }
7802 EMSG2(_(e_intern2), "get_var_special_name()");
7803 return "42";
7804}
7805
Bram Moolenaar8c711452005-01-14 21:53:12 +00007806/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007807 * Return a string with the string representation of a variable.
7808 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007809 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007810 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007811 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007812 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 */
7814 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007815echo_string(
7816 typval_T *tv,
7817 char_u **tofree,
7818 char_u *numbuf,
7819 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821 static int recurse = 0;
7822 char_u *r = NULL;
7823
Bram Moolenaar33570922005-01-25 22:26:29 +00007824 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007826 if (!did_echo_string_emsg)
7827 {
7828 /* Only give this message once for a recursive call to avoid
7829 * flooding the user with errors. And stop iterating over lists
7830 * and dicts. */
7831 did_echo_string_emsg = TRUE;
7832 EMSG(_("E724: variable nested too deep for displaying"));
7833 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007834 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007835 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007836 }
7837 ++recurse;
7838
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007839 switch (tv->v_type)
7840 {
7841 case VAR_FUNC:
7842 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007843 r = tv->vval.v_string;
7844 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007846 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007847 if (tv->vval.v_list == NULL)
7848 {
7849 *tofree = NULL;
7850 r = NULL;
7851 }
7852 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7853 {
7854 *tofree = NULL;
7855 r = (char_u *)"[...]";
7856 }
7857 else
7858 {
7859 tv->vval.v_list->lv_copyID = copyID;
7860 *tofree = list2string(tv, copyID);
7861 r = *tofree;
7862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007864
Bram Moolenaar8c711452005-01-14 21:53:12 +00007865 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007866 if (tv->vval.v_dict == NULL)
7867 {
7868 *tofree = NULL;
7869 r = NULL;
7870 }
7871 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7872 {
7873 *tofree = NULL;
7874 r = (char_u *)"{...}";
7875 }
7876 else
7877 {
7878 tv->vval.v_dict->dv_copyID = copyID;
7879 *tofree = dict2string(tv, copyID);
7880 r = *tofree;
7881 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884 case VAR_STRING:
7885 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007886 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007887 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007888 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007889 *tofree = NULL;
7890 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007891 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007892
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007893 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007894#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007895 *tofree = NULL;
7896 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7897 r = numbuf;
7898 break;
7899#endif
7900
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007901 case VAR_SPECIAL:
7902 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007903 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007904 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906
Bram Moolenaar8502c702014-06-17 12:51:16 +02007907 if (--recurse == 0)
7908 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007909 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910}
7911
7912/*
7913 * Return a string with the string representation of a variable.
7914 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7915 * "numbuf" is used for a number.
7916 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007917 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007918 */
7919 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007920tv2string(
7921 typval_T *tv,
7922 char_u **tofree,
7923 char_u *numbuf,
7924 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007925{
7926 switch (tv->v_type)
7927 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007928 case VAR_FUNC:
7929 *tofree = string_quote(tv->vval.v_string, TRUE);
7930 return *tofree;
7931 case VAR_STRING:
7932 *tofree = string_quote(tv->vval.v_string, FALSE);
7933 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007934#ifdef FEAT_FLOAT
7935 case VAR_FLOAT:
7936 *tofree = NULL;
7937 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7938 return numbuf;
7939#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007940 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007941 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007942 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007943 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007944 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007945 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007946 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007947 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007948 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007949 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007950}
7951
7952/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007953 * Return string "str" in ' quotes, doubling ' characters.
7954 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007956 */
7957 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007958string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959{
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007961 char_u *p, *r, *s;
7962
Bram Moolenaar33570922005-01-25 22:26:29 +00007963 len = (function ? 13 : 3);
7964 if (str != NULL)
7965 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007966 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007967 for (p = str; *p != NUL; mb_ptr_adv(p))
7968 if (*p == '\'')
7969 ++len;
7970 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007971 s = r = alloc(len);
7972 if (r != NULL)
7973 {
7974 if (function)
7975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007976 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007977 r += 10;
7978 }
7979 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007980 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007981 if (str != NULL)
7982 for (p = str; *p != NUL; )
7983 {
7984 if (*p == '\'')
7985 *r++ = '\'';
7986 MB_COPY_CHAR(p, r);
7987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007988 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007989 if (function)
7990 *r++ = ')';
7991 *r++ = NUL;
7992 }
7993 return s;
7994}
7995
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007996#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007997/*
7998 * Convert the string "text" to a floating point number.
7999 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8000 * this always uses a decimal point.
8001 * Returns the length of the text that was consumed.
8002 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008004string2float(
8005 char_u *text,
8006 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008007{
8008 char *s = (char *)text;
8009 float_T f;
8010
8011 f = strtod(s, &s);
8012 *value = f;
8013 return (int)((char_u *)s - text);
8014}
8015#endif
8016
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 * Get the value of an environment variable.
8019 * "arg" is pointing to the '$'. It is advanced to after the name.
8020 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008021 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 */
8023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008024get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025{
8026 char_u *string = NULL;
8027 int len;
8028 int cc;
8029 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008030 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031
8032 ++*arg;
8033 name = *arg;
8034 len = get_env_len(arg);
8035 if (evaluate)
8036 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008037 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008038 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008039
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008040 cc = name[len];
8041 name[len] = NUL;
8042 /* first try vim_getenv(), fast for normal environment vars */
8043 string = vim_getenv(name, &mustfree);
8044 if (string != NULL && *string != NUL)
8045 {
8046 if (!mustfree)
8047 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008049 else
8050 {
8051 if (mustfree)
8052 vim_free(string);
8053
8054 /* next try expanding things like $VIM and ${HOME} */
8055 string = expand_env_save(name - 1);
8056 if (string != NULL && *string == '$')
8057 {
8058 vim_free(string);
8059 string = NULL;
8060 }
8061 }
8062 name[len] = cc;
8063
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008064 rettv->v_type = VAR_STRING;
8065 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 }
8067
8068 return OK;
8069}
8070
8071/*
8072 * Array with names and number of arguments of all internal functions
8073 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8074 */
8075static struct fst
8076{
8077 char *f_name; /* function name */
8078 char f_min_argc; /* minimal number of arguments */
8079 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008080 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008081 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082} functions[] =
8083{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008084#ifdef FEAT_FLOAT
8085 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008086 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008087#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008088 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008089 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008090 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"append", 2, 2, f_append},
8092 {"argc", 0, 0, f_argc},
8093 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008094 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008095 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008096#ifdef FEAT_FLOAT
8097 {"asin", 1, 1, f_asin}, /* WJMc */
8098#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008099 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008100 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008101 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008102 {"assert_false", 1, 2, f_assert_false},
8103 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008104#ifdef FEAT_FLOAT
8105 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008106 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008109 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"bufexists", 1, 1, f_bufexists},
8111 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8112 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8113 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8114 {"buflisted", 1, 1, f_buflisted},
8115 {"bufloaded", 1, 1, f_bufloaded},
8116 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008117 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"bufwinnr", 1, 1, f_bufwinnr},
8119 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008120 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008121 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008122 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008123#ifdef FEAT_FLOAT
8124 {"ceil", 1, 1, f_ceil},
8125#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008126#ifdef FEAT_CHANNEL
8127 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008128 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008129 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008130 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008131 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008132 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8133 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar77073442016-02-13 23:23:53 +01008134 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008135#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008136 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008137 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008139 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008141#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008142 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008143 {"complete_add", 1, 1, f_complete_add},
8144 {"complete_check", 0, 0, f_complete_check},
8145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008147 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008148#ifdef FEAT_FLOAT
8149 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008150 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008151#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008152 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008154 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008155 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008156 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008158 {"diff_filler", 1, 1, f_diff_filler},
8159 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008160 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008161 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008163 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"eventhandler", 0, 0, f_eventhandler},
8165 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008166 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008168#ifdef FEAT_FLOAT
8169 {"exp", 1, 1, f_exp},
8170#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008171 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008172 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008173 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8175 {"filereadable", 1, 1, f_filereadable},
8176 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008177 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008178 {"finddir", 1, 3, f_finddir},
8179 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008180#ifdef FEAT_FLOAT
8181 {"float2nr", 1, 1, f_float2nr},
8182 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008183 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008184#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008185 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"fnamemodify", 2, 2, f_fnamemodify},
8187 {"foldclosed", 1, 1, f_foldclosed},
8188 {"foldclosedend", 1, 1, f_foldclosedend},
8189 {"foldlevel", 1, 1, f_foldlevel},
8190 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008191 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008193 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008194 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008195 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008196 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008197 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"getchar", 0, 1, f_getchar},
8199 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008200 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {"getcmdline", 0, 0, f_getcmdline},
8202 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008203 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008204 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008205 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008206 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008207 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008208 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {"getfsize", 1, 1, f_getfsize},
8210 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008211 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008212 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008213 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008214 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008215 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008216 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008217 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008218 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008220 {"gettabvar", 2, 3, f_gettabvar},
8221 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {"getwinposx", 0, 0, f_getwinposx},
8223 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008224 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008225 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008226 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008227 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008229 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008230 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008231 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8233 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8234 {"histadd", 2, 2, f_histadd},
8235 {"histdel", 1, 2, f_histdel},
8236 {"histget", 1, 2, f_histget},
8237 {"histnr", 1, 1, f_histnr},
8238 {"hlID", 1, 1, f_hlID},
8239 {"hlexists", 1, 1, f_hlexists},
8240 {"hostname", 0, 0, f_hostname},
8241 {"iconv", 3, 3, f_iconv},
8242 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008243 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008244 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008246 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {"inputrestore", 0, 0, f_inputrestore},
8248 {"inputsave", 0, 0, f_inputsave},
8249 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008250 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008251 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008253 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008254 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008255#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008256# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008257 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008258# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008259 {"job_start", 1, 2, f_job_start},
8260 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008261 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008262#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008263 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008264 {"js_decode", 1, 1, f_js_decode},
8265 {"js_encode", 1, 1, f_js_encode},
8266 {"json_decode", 1, 1, f_json_decode},
8267 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008268 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008270 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {"libcall", 3, 3, f_libcall},
8272 {"libcallnr", 3, 3, f_libcallnr},
8273 {"line", 1, 1, f_line},
8274 {"line2byte", 1, 1, f_line2byte},
8275 {"lispindent", 1, 1, f_lispindent},
8276 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008277#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008278 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279 {"log10", 1, 1, f_log10},
8280#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008281#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008282 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008283#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008284 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008285 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008286 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008287 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008288 {"matchadd", 2, 5, f_matchadd},
8289 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008290 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008291 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008292 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008293 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008294 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008295 {"max", 1, 1, f_max},
8296 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008297#ifdef vim_mkdir
8298 {"mkdir", 1, 3, f_mkdir},
8299#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008300 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008301#ifdef FEAT_MZSCHEME
8302 {"mzeval", 1, 1, f_mzeval},
8303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008305 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008306 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008307 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008308#ifdef FEAT_PERL
8309 {"perleval", 1, 1, f_perleval},
8310#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008311#ifdef FEAT_FLOAT
8312 {"pow", 2, 2, f_pow},
8313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008315 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008316 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008317#ifdef FEAT_PYTHON3
8318 {"py3eval", 1, 1, f_py3eval},
8319#endif
8320#ifdef FEAT_PYTHON
8321 {"pyeval", 1, 1, f_pyeval},
8322#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008323 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008324 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008325 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008326 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008327 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"remote_expr", 2, 3, f_remote_expr},
8329 {"remote_foreground", 1, 1, f_remote_foreground},
8330 {"remote_peek", 1, 2, f_remote_peek},
8331 {"remote_read", 1, 1, f_remote_read},
8332 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008333 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008335 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008337 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008338#ifdef FEAT_FLOAT
8339 {"round", 1, 1, f_round},
8340#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008341 {"screenattr", 2, 2, f_screenattr},
8342 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008343 {"screencol", 0, 0, f_screencol},
8344 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008345 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008346 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008347 {"searchpair", 3, 7, f_searchpair},
8348 {"searchpairpos", 3, 7, f_searchpairpos},
8349 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"server2client", 2, 2, f_server2client},
8351 {"serverlist", 0, 0, f_serverlist},
8352 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008353 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {"setcmdpos", 1, 1, f_setcmdpos},
8355 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008356 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008357 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008358 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008359 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008361 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008362 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008364#ifdef FEAT_CRYPT
8365 {"sha256", 1, 1, f_sha256},
8366#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008367 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008368 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370#ifdef FEAT_FLOAT
8371 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008372 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008373#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008374 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008375 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008376 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008377 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008378 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008379#ifdef FEAT_FLOAT
8380 {"sqrt", 1, 1, f_sqrt},
8381 {"str2float", 1, 1, f_str2float},
8382#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008383 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008384 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008385 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386#ifdef HAVE_STRFTIME
8387 {"strftime", 1, 2, f_strftime},
8388#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008389 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008390 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 {"strlen", 1, 1, f_strlen},
8392 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008393 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008395 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008396 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"substitute", 4, 4, f_substitute},
8398 {"synID", 3, 3, f_synID},
8399 {"synIDattr", 2, 3, f_synIDattr},
8400 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008401 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008402 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008403 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008404 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008405 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008406 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008407 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008408 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008409 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008410#ifdef FEAT_FLOAT
8411 {"tan", 1, 1, f_tan},
8412 {"tanh", 1, 1, f_tanh},
8413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008415 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"tolower", 1, 1, f_tolower},
8417 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008418 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008419#ifdef FEAT_FLOAT
8420 {"trunc", 1, 1, f_trunc},
8421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008423 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008424 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008425 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008426 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {"virtcol", 1, 1, f_virtcol},
8428 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008429 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"winbufnr", 1, 1, f_winbufnr},
8431 {"wincol", 0, 0, f_wincol},
8432 {"winheight", 1, 1, f_winheight},
8433 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008434 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008436 {"winrestview", 1, 1, f_winrestview},
8437 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008439 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008440 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008441 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442};
8443
8444#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8445
8446/*
8447 * Function given to ExpandGeneric() to obtain the list of internal
8448 * or user defined function names.
8449 */
8450 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008451get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 static int intidx = -1;
8454 char_u *name;
8455
8456 if (idx == 0)
8457 intidx = -1;
8458 if (intidx < 0)
8459 {
8460 name = get_user_func_name(xp, idx);
8461 if (name != NULL)
8462 return name;
8463 }
8464 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8465 {
8466 STRCPY(IObuff, functions[intidx].f_name);
8467 STRCAT(IObuff, "(");
8468 if (functions[intidx].f_max_argc == 0)
8469 STRCAT(IObuff, ")");
8470 return IObuff;
8471 }
8472
8473 return NULL;
8474}
8475
8476/*
8477 * Function given to ExpandGeneric() to obtain the list of internal or
8478 * user defined variable or function names.
8479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008481get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
8483 static int intidx = -1;
8484 char_u *name;
8485
8486 if (idx == 0)
8487 intidx = -1;
8488 if (intidx < 0)
8489 {
8490 name = get_function_name(xp, idx);
8491 if (name != NULL)
8492 return name;
8493 }
8494 return get_user_var_name(xp, ++intidx);
8495}
8496
8497#endif /* FEAT_CMDL_COMPL */
8498
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008499#if defined(EBCDIC) || defined(PROTO)
8500/*
8501 * Compare struct fst by function name.
8502 */
8503 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008504compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008505{
8506 struct fst *p1 = (struct fst *)s1;
8507 struct fst *p2 = (struct fst *)s2;
8508
8509 return STRCMP(p1->f_name, p2->f_name);
8510}
8511
8512/*
8513 * Sort the function table by function name.
8514 * The sorting of the table above is ASCII dependant.
8515 * On machines using EBCDIC we have to sort it.
8516 */
8517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008518sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008519{
8520 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8521
8522 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8523}
8524#endif
8525
8526
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527/*
8528 * Find internal function in table above.
8529 * Return index, or -1 if not found
8530 */
8531 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008532find_internal_func(
8533 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 int first = 0;
8536 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8537 int cmp;
8538 int x;
8539
8540 /*
8541 * Find the function name in the table. Binary search.
8542 */
8543 while (first <= last)
8544 {
8545 x = first + ((unsigned)(last - first) >> 1);
8546 cmp = STRCMP(name, functions[x].f_name);
8547 if (cmp < 0)
8548 last = x - 1;
8549 else if (cmp > 0)
8550 first = x + 1;
8551 else
8552 return x;
8553 }
8554 return -1;
8555}
8556
8557/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008558 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8559 * name it contains, otherwise return "name".
8560 */
8561 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008562deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008563{
Bram Moolenaar33570922005-01-25 22:26:29 +00008564 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008565 int cc;
8566
8567 cc = name[*lenp];
8568 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008569 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008570 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008572 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 {
8575 *lenp = 0;
8576 return (char_u *)""; /* just in case */
8577 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008578 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008579 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008580 }
8581
8582 return name;
8583}
8584
8585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 * Allocate a variable for the result of a function.
8587 * Return OK or FAIL.
8588 */
8589 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008590get_func_tv(
8591 char_u *name, /* name of the function */
8592 int len, /* length of "name" */
8593 typval_T *rettv,
8594 char_u **arg, /* argument, pointing to the '(' */
8595 linenr_T firstline, /* first line of range */
8596 linenr_T lastline, /* last line of range */
8597 int *doesrange, /* return: function handled range */
8598 int evaluate,
8599 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600{
8601 char_u *argp;
8602 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008603 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 int argcount = 0; /* number of arguments found */
8605
8606 /*
8607 * Get the arguments.
8608 */
8609 argp = *arg;
8610 while (argcount < MAX_FUNC_ARGS)
8611 {
8612 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8613 if (*argp == ')' || *argp == ',' || *argp == NUL)
8614 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8616 {
8617 ret = FAIL;
8618 break;
8619 }
8620 ++argcount;
8621 if (*argp != ',')
8622 break;
8623 }
8624 if (*argp == ')')
8625 ++argp;
8626 else
8627 ret = FAIL;
8628
8629 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008631 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008633 {
8634 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008635 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008637 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639
8640 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642
8643 *arg = skipwhite(argp);
8644 return ret;
8645}
8646
8647
8648/*
8649 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008650 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008651 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008654call_func(
8655 char_u *funcname, /* name of the function */
8656 int len, /* length of "name" */
8657 typval_T *rettv, /* return value goes here */
8658 int argcount, /* number of "argvars" */
8659 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008660 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008661 linenr_T firstline, /* first line of range */
8662 linenr_T lastline, /* last line of range */
8663 int *doesrange, /* return: function handled range */
8664 int evaluate,
8665 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666{
8667 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668#define ERROR_UNKNOWN 0
8669#define ERROR_TOOMANY 1
8670#define ERROR_TOOFEW 2
8671#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008672#define ERROR_DICT 4
8673#define ERROR_NONE 5
8674#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 int error = ERROR_NONE;
8676 int i;
8677 int llen;
8678 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679#define FLEN_FIXED 40
8680 char_u fname_buf[FLEN_FIXED + 1];
8681 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008682 char_u *name;
8683
8684 /* Make a copy of the name, if it comes from a funcref variable it could
8685 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008686 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008687 if (name == NULL)
8688 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689
8690 /*
8691 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8692 * Change <SNR>123_name() to K_SNR 123_name().
8693 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 llen = eval_fname_script(name);
8696 if (llen > 0)
8697 {
8698 fname_buf[0] = K_SPECIAL;
8699 fname_buf[1] = KS_EXTRA;
8700 fname_buf[2] = (int)KE_SNR;
8701 i = 3;
8702 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8703 {
8704 if (current_SID <= 0)
8705 error = ERROR_SCRIPT;
8706 else
8707 {
8708 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8709 i = (int)STRLEN(fname_buf);
8710 }
8711 }
8712 if (i + STRLEN(name + llen) < FLEN_FIXED)
8713 {
8714 STRCPY(fname_buf + i, name + llen);
8715 fname = fname_buf;
8716 }
8717 else
8718 {
8719 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8720 if (fname == NULL)
8721 error = ERROR_OTHER;
8722 else
8723 {
8724 mch_memmove(fname, fname_buf, (size_t)i);
8725 STRCPY(fname + i, name + llen);
8726 }
8727 }
8728 }
8729 else
8730 fname = name;
8731
8732 *doesrange = FALSE;
8733
8734
8735 /* execute the function if no errors detected and executing */
8736 if (evaluate && error == ERROR_NONE)
8737 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008738 char_u *rfname = fname;
8739
8740 /* Ignore "g:" before a function name. */
8741 if (fname[0] == 'g' && fname[1] == ':')
8742 rfname = fname + 2;
8743
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008744 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8745 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 error = ERROR_UNKNOWN;
8747
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008748 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {
8750 /*
8751 * User defined function.
8752 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008753 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008754
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008756 /* Trigger FuncUndefined event, may load the function. */
8757 if (fp == NULL
8758 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008759 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008760 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008762 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008763 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 }
8765#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008766 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008767 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008768 {
8769 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008770 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008771 }
8772
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 if (fp != NULL)
8774 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008775 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008777 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008779 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008781 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008782 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 else
8784 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008785 int did_save_redo = FALSE;
8786
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 /*
8788 * Call the user function.
8789 * Save and restore search patterns, script variables and
8790 * redo buffer.
8791 */
8792 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008793#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008794 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008795#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008796 {
8797 saveRedobuff();
8798 did_save_redo = TRUE;
8799 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008800 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008802 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008803 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8804 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8805 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008806 /* Function was unreferenced while being used, free it
8807 * now. */
8808 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008809 if (did_save_redo)
8810 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 restore_search_patterns();
8812 error = ERROR_NONE;
8813 }
8814 }
8815 }
8816 else
8817 {
8818 /*
8819 * Find the function name in the table, call its implementation.
8820 */
8821 i = find_internal_func(fname);
8822 if (i >= 0)
8823 {
8824 if (argcount < functions[i].f_min_argc)
8825 error = ERROR_TOOFEW;
8826 else if (argcount > functions[i].f_max_argc)
8827 error = ERROR_TOOMANY;
8828 else
8829 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008830 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 error = ERROR_NONE;
8833 }
8834 }
8835 }
8836 /*
8837 * The function call (or "FuncUndefined" autocommand sequence) might
8838 * have been aborted by an error, an interrupt, or an explicitly thrown
8839 * exception that has not been caught so far. This situation can be
8840 * tested for by calling aborting(). For an error in an internal
8841 * function or for the "E132" error in call_user_func(), however, the
8842 * throw point at which the "force_abort" flag (temporarily reset by
8843 * emsg()) is normally updated has not been reached yet. We need to
8844 * update that flag first to make aborting() reliable.
8845 */
8846 update_force_abort();
8847 }
8848 if (error == ERROR_NONE)
8849 ret = OK;
8850
8851 /*
8852 * Report an error unless the argument evaluation or function call has been
8853 * cancelled due to an aborting error, an interrupt, or an exception.
8854 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008855 if (!aborting())
8856 {
8857 switch (error)
8858 {
8859 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008860 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008861 break;
8862 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008863 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008864 break;
8865 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008866 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008867 name);
8868 break;
8869 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008870 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008871 name);
8872 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008873 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008874 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 name);
8876 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008877 }
8878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 if (fname != name && fname != fname_buf)
8881 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008882 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883
8884 return ret;
8885}
8886
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008887/*
8888 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008889 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008890 */
8891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008892emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008893{
8894 char_u *p;
8895
8896 if (*name == K_SPECIAL)
8897 p = concat_str((char_u *)"<SNR>", name + 3);
8898 else
8899 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008900 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008901 if (p != name)
8902 vim_free(p);
8903}
8904
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008905/*
8906 * Return TRUE for a non-zero Number and a non-empty String.
8907 */
8908 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008909non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008910{
8911 return ((argvars[0].v_type == VAR_NUMBER
8912 && argvars[0].vval.v_number != 0)
8913 || (argvars[0].v_type == VAR_STRING
8914 && argvars[0].vval.v_string != NULL
8915 && *argvars[0].vval.v_string != NUL));
8916}
8917
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918/*********************************************
8919 * Implementation of the built-in functions
8920 */
8921
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008922#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008923static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008924
8925/*
8926 * Get the float value of "argvars[0]" into "f".
8927 * Returns FAIL when the argument is not a Number or Float.
8928 */
8929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008930get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008931{
8932 if (argvars[0].v_type == VAR_FLOAT)
8933 {
8934 *f = argvars[0].vval.v_float;
8935 return OK;
8936 }
8937 if (argvars[0].v_type == VAR_NUMBER)
8938 {
8939 *f = (float_T)argvars[0].vval.v_number;
8940 return OK;
8941 }
8942 EMSG(_("E808: Number or Float required"));
8943 return FAIL;
8944}
8945
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008946/*
8947 * "abs(expr)" function
8948 */
8949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008950f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008951{
8952 if (argvars[0].v_type == VAR_FLOAT)
8953 {
8954 rettv->v_type = VAR_FLOAT;
8955 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8956 }
8957 else
8958 {
8959 varnumber_T n;
8960 int error = FALSE;
8961
8962 n = get_tv_number_chk(&argvars[0], &error);
8963 if (error)
8964 rettv->vval.v_number = -1;
8965 else if (n > 0)
8966 rettv->vval.v_number = n;
8967 else
8968 rettv->vval.v_number = -n;
8969 }
8970}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008971
8972/*
8973 * "acos()" function
8974 */
8975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008976f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008977{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01008978 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008979
8980 rettv->v_type = VAR_FLOAT;
8981 if (get_float_arg(argvars, &f) == OK)
8982 rettv->vval.v_float = acos(f);
8983 else
8984 rettv->vval.v_float = 0.0;
8985}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008986#endif
8987
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 */
8991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008992f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
Bram Moolenaar33570922005-01-25 22:26:29 +00008994 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008997 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008999 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009000 && !tv_check_lock(l->lv_lock,
9001 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009002 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009003 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009004 }
9005 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009006 EMSG(_(e_listreq));
9007}
9008
9009/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009010 * "alloc_fail(id, countdown, repeat)" function
9011 */
9012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009013f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009014{
9015 if (argvars[0].v_type != VAR_NUMBER
9016 || argvars[0].vval.v_number <= 0
9017 || argvars[1].v_type != VAR_NUMBER
9018 || argvars[1].vval.v_number < 0
9019 || argvars[2].v_type != VAR_NUMBER)
9020 EMSG(_(e_invarg));
9021 else
9022 {
9023 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009024 if (alloc_fail_id >= aid_last)
9025 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009026 alloc_fail_countdown = argvars[1].vval.v_number;
9027 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009028 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009029 }
9030}
9031
9032/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009033 * "and(expr, expr)" function
9034 */
9035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009036f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009037{
9038 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9039 & get_tv_number_chk(&argvars[1], NULL);
9040}
9041
9042/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009043 * "append(lnum, string/list)" function
9044 */
9045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009046f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009047{
9048 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009050 list_T *l = NULL;
9051 listitem_T *li = NULL;
9052 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009053 long added = 0;
9054
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009055 /* When coming here from Insert mode, sync undo, so that this can be
9056 * undone separately from what was previously inserted. */
9057 if (u_sync_once == 2)
9058 {
9059 u_sync_once = 1; /* notify that u_sync() was called */
9060 u_sync(TRUE);
9061 }
9062
Bram Moolenaar0d660222005-01-07 21:51:51 +00009063 lnum = get_tv_lnum(argvars);
9064 if (lnum >= 0
9065 && lnum <= curbuf->b_ml.ml_line_count
9066 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009067 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009068 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009069 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009070 l = argvars[1].vval.v_list;
9071 if (l == NULL)
9072 return;
9073 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009074 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009075 for (;;)
9076 {
9077 if (l == NULL)
9078 tv = &argvars[1]; /* append a string */
9079 else if (li == NULL)
9080 break; /* end of list */
9081 else
9082 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009083 line = get_tv_string_chk(tv);
9084 if (line == NULL) /* type error */
9085 {
9086 rettv->vval.v_number = 1; /* Failed */
9087 break;
9088 }
9089 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009090 ++added;
9091 if (l == NULL)
9092 break;
9093 li = li->li_next;
9094 }
9095
9096 appended_lines_mark(lnum, added);
9097 if (curwin->w_cursor.lnum > lnum)
9098 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009100 else
9101 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102}
9103
9104/*
9105 * "argc()" function
9106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009108f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111}
9112
9113/*
9114 * "argidx()" function
9115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009117f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009119 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120}
9121
9122/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009123 * "arglistid()" function
9124 */
9125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009126f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009127{
9128 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009129
9130 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009131 wp = find_tabwin(&argvars[0], &argvars[1]);
9132 if (wp != NULL)
9133 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009134}
9135
9136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 * "argv(nr)" function
9138 */
9139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009140f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141{
9142 int idx;
9143
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009144 if (argvars[0].v_type != VAR_UNKNOWN)
9145 {
9146 idx = get_tv_number_chk(&argvars[0], NULL);
9147 if (idx >= 0 && idx < ARGCOUNT)
9148 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9149 else
9150 rettv->vval.v_string = NULL;
9151 rettv->v_type = VAR_STRING;
9152 }
9153 else if (rettv_list_alloc(rettv) == OK)
9154 for (idx = 0; idx < ARGCOUNT; ++idx)
9155 list_append_string(rettv->vval.v_list,
9156 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157}
9158
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009159static void prepare_assert_error(garray_T*gap);
9160static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9161static void assert_error(garray_T *gap);
9162static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009163
9164/*
9165 * Prepare "gap" for an assert error and add the sourcing position.
9166 */
9167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009168prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009169{
9170 char buf[NUMBUFLEN];
9171
9172 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009173 if (sourcing_name != NULL)
9174 {
9175 ga_concat(gap, sourcing_name);
9176 if (sourcing_lnum > 0)
9177 ga_concat(gap, (char_u *)" ");
9178 }
9179 if (sourcing_lnum > 0)
9180 {
9181 sprintf(buf, "line %ld", (long)sourcing_lnum);
9182 ga_concat(gap, (char_u *)buf);
9183 }
9184 if (sourcing_name != NULL || sourcing_lnum > 0)
9185 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009186}
9187
9188/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009189 * Append "str" to "gap", escaping unprintable characters.
9190 * Changes NL to \n, CR to \r, etc.
9191 */
9192 static void
9193ga_concat_esc(garray_T *gap, char_u *str)
9194{
9195 char_u *p;
9196 char_u buf[NUMBUFLEN];
9197
9198 for (p = str; *p != NUL; ++p)
9199 switch (*p)
9200 {
9201 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9202 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9203 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9204 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9205 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9206 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9207 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9208 default:
9209 if (*p < ' ')
9210 {
9211 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9212 ga_concat(gap, buf);
9213 }
9214 else
9215 ga_append(gap, *p);
9216 break;
9217 }
9218}
9219
9220/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009221 * Fill "gap" with information about an assert error.
9222 */
9223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009224fill_assert_error(
9225 garray_T *gap,
9226 typval_T *opt_msg_tv,
9227 char_u *exp_str,
9228 typval_T *exp_tv,
9229 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009230{
9231 char_u numbuf[NUMBUFLEN];
9232 char_u *tofree;
9233
9234 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9235 {
9236 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9237 vim_free(tofree);
9238 }
9239 else
9240 {
9241 ga_concat(gap, (char_u *)"Expected ");
9242 if (exp_str == NULL)
9243 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009244 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009245 vim_free(tofree);
9246 }
9247 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009248 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009249 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009250 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009251 vim_free(tofree);
9252 }
9253}
Bram Moolenaar43345542015-11-29 17:35:35 +01009254
9255/*
9256 * Add an assert error to v:errors.
9257 */
9258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009259assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009260{
9261 struct vimvar *vp = &vimvars[VV_ERRORS];
9262
9263 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9264 /* Make sure v:errors is a list. */
9265 set_vim_var_list(VV_ERRORS, list_alloc());
9266 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9267}
9268
Bram Moolenaar43345542015-11-29 17:35:35 +01009269/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009270 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009271 */
9272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009273f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009274{
9275 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009276
9277 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9278 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009279 prepare_assert_error(&ga);
9280 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9281 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009282 ga_clear(&ga);
9283 }
9284}
9285
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009286/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009287 * "assert_exception(string[, msg])" function
9288 */
9289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009290f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009291{
9292 garray_T ga;
9293 char *error;
9294
9295 error = (char *)get_tv_string_chk(&argvars[0]);
9296 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9297 {
9298 prepare_assert_error(&ga);
9299 ga_concat(&ga, (char_u *)"v:exception is not set");
9300 assert_error(&ga);
9301 ga_clear(&ga);
9302 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009303 else if (error != NULL
9304 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009305 {
9306 prepare_assert_error(&ga);
9307 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9308 &vimvars[VV_EXCEPTION].vv_tv);
9309 assert_error(&ga);
9310 ga_clear(&ga);
9311 }
9312}
9313
9314/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009315 * "assert_fails(cmd [, error])" function
9316 */
9317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009318f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009319{
9320 char_u *cmd = get_tv_string_chk(&argvars[0]);
9321 garray_T ga;
9322
9323 called_emsg = FALSE;
9324 suppress_errthrow = TRUE;
9325 emsg_silent = TRUE;
9326 do_cmdline_cmd(cmd);
9327 if (!called_emsg)
9328 {
9329 prepare_assert_error(&ga);
9330 ga_concat(&ga, (char_u *)"command did not fail: ");
9331 ga_concat(&ga, cmd);
9332 assert_error(&ga);
9333 ga_clear(&ga);
9334 }
9335 else if (argvars[1].v_type != VAR_UNKNOWN)
9336 {
9337 char_u buf[NUMBUFLEN];
9338 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9339
9340 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9341 {
9342 prepare_assert_error(&ga);
9343 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9344 &vimvars[VV_ERRMSG].vv_tv);
9345 assert_error(&ga);
9346 ga_clear(&ga);
9347 }
9348 }
9349
9350 called_emsg = FALSE;
9351 suppress_errthrow = FALSE;
9352 emsg_silent = FALSE;
9353 emsg_on_display = FALSE;
9354 set_vim_var_string(VV_ERRMSG, NULL, 0);
9355}
9356
9357/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009358 * Common for assert_true() and assert_false().
9359 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009361assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009362{
9363 int error = FALSE;
9364 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009365
Bram Moolenaar37127922016-02-06 20:29:28 +01009366 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009367 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009368 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009369 if (argvars[0].v_type != VAR_NUMBER
9370 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9371 || error)
9372 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009373 prepare_assert_error(&ga);
9374 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009375 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009376 NULL, &argvars[0]);
9377 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009378 ga_clear(&ga);
9379 }
9380}
9381
9382/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009383 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009384 */
9385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009386f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009387{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009388 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009389}
9390
9391/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009392 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009393 */
9394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009395f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009396{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009397 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009398}
9399
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009400#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009401/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009402 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009403 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009405f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009406{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009407 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009408
9409 rettv->v_type = VAR_FLOAT;
9410 if (get_float_arg(argvars, &f) == OK)
9411 rettv->vval.v_float = asin(f);
9412 else
9413 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009414}
9415
9416/*
9417 * "atan()" function
9418 */
9419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009420f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009421{
9422 float_T f;
9423
9424 rettv->v_type = VAR_FLOAT;
9425 if (get_float_arg(argvars, &f) == OK)
9426 rettv->vval.v_float = atan(f);
9427 else
9428 rettv->vval.v_float = 0.0;
9429}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009430
9431/*
9432 * "atan2()" function
9433 */
9434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009435f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009436{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009437 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009438
9439 rettv->v_type = VAR_FLOAT;
9440 if (get_float_arg(argvars, &fx) == OK
9441 && get_float_arg(&argvars[1], &fy) == OK)
9442 rettv->vval.v_float = atan2(fx, fy);
9443 else
9444 rettv->vval.v_float = 0.0;
9445}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009446#endif
9447
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448/*
9449 * "browse(save, title, initdir, default)" function
9450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009452f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453{
9454#ifdef FEAT_BROWSE
9455 int save;
9456 char_u *title;
9457 char_u *initdir;
9458 char_u *defname;
9459 char_u buf[NUMBUFLEN];
9460 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009461 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463 save = get_tv_number_chk(&argvars[0], &error);
9464 title = get_tv_string_chk(&argvars[1]);
9465 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9466 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009468 if (error || title == NULL || initdir == NULL || defname == NULL)
9469 rettv->vval.v_string = NULL;
9470 else
9471 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009472 do_browse(save ? BROWSE_SAVE : 0,
9473 title, defname, NULL, initdir, NULL, curbuf);
9474#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009476#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009478}
9479
9480/*
9481 * "browsedir(title, initdir)" function
9482 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009484f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009485{
9486#ifdef FEAT_BROWSE
9487 char_u *title;
9488 char_u *initdir;
9489 char_u buf[NUMBUFLEN];
9490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009491 title = get_tv_string_chk(&argvars[0]);
9492 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009494 if (title == NULL || initdir == NULL)
9495 rettv->vval.v_string = NULL;
9496 else
9497 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009498 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503}
9504
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009505static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009506
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507/*
9508 * Find a buffer by number or exact name.
9509 */
9510 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009511find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512{
9513 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009515 if (avar->v_type == VAR_NUMBER)
9516 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009517 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009519 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009520 if (buf == NULL)
9521 {
9522 /* No full path name match, try a match with a URL or a "nofile"
9523 * buffer, these don't use the full path. */
9524 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9525 if (buf->b_fname != NULL
9526 && (path_with_url(buf->b_fname)
9527#ifdef FEAT_QUICKFIX
9528 || bt_nofile(buf)
9529#endif
9530 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009531 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009532 break;
9533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 }
9535 return buf;
9536}
9537
9538/*
9539 * "bufexists(expr)" function
9540 */
9541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009542f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545}
9546
9547/*
9548 * "buflisted(expr)" function
9549 */
9550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009551f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553 buf_T *buf;
9554
9555 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557}
9558
9559/*
9560 * "bufloaded(expr)" function
9561 */
9562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009563f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 buf_T *buf;
9566
9567 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009571static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009572
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573/*
9574 * Get buffer by number or pattern.
9575 */
9576 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009577get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 int save_magic;
9581 char_u *save_cpo;
9582 buf_T *buf;
9583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 if (tv->v_type == VAR_NUMBER)
9585 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009586 if (tv->v_type != VAR_STRING)
9587 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 if (name == NULL || *name == NUL)
9589 return curbuf;
9590 if (name[0] == '$' && name[1] == NUL)
9591 return lastbuf;
9592
9593 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9594 save_magic = p_magic;
9595 p_magic = TRUE;
9596 save_cpo = p_cpo;
9597 p_cpo = (char_u *)"";
9598
9599 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009600 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601
9602 p_magic = save_magic;
9603 p_cpo = save_cpo;
9604
9605 /* If not found, try expanding the name, like done for bufexists(). */
9606 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009607 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608
9609 return buf;
9610}
9611
9612/*
9613 * "bufname(expr)" function
9614 */
9615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009616f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617{
9618 buf_T *buf;
9619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009622 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 --emsg_off;
9629}
9630
9631/*
9632 * "bufnr(expr)" function
9633 */
9634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009635f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
9637 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009638 int error = FALSE;
9639 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009641 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009643 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009644 --emsg_off;
9645
9646 /* If the buffer isn't found and the second argument is not zero create a
9647 * new buffer. */
9648 if (buf == NULL
9649 && argvars[1].v_type != VAR_UNKNOWN
9650 && get_tv_number_chk(&argvars[1], &error) != 0
9651 && !error
9652 && (name = get_tv_string_chk(&argvars[0])) != NULL
9653 && !error)
9654 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9655
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660}
9661
9662/*
9663 * "bufwinnr(nr)" function
9664 */
9665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009666f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
9668#ifdef FEAT_WINDOWS
9669 win_T *wp;
9670 int winnr = 0;
9671#endif
9672 buf_T *buf;
9673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009674 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009676 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677#ifdef FEAT_WINDOWS
9678 for (wp = firstwin; wp; wp = wp->w_next)
9679 {
9680 ++winnr;
9681 if (wp->w_buffer == buf)
9682 break;
9683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687#endif
9688 --emsg_off;
9689}
9690
9691/*
9692 * "byte2line(byte)" function
9693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009695f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696{
9697#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699#else
9700 long boff = 0;
9701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009702 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 (linenr_T)0, &boff);
9708#endif
9709}
9710
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009712byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009713{
9714#ifdef FEAT_MBYTE
9715 char_u *t;
9716#endif
9717 char_u *str;
9718 long idx;
9719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009720 str = get_tv_string_chk(&argvars[0]);
9721 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009723 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009724 return;
9725
9726#ifdef FEAT_MBYTE
9727 t = str;
9728 for ( ; idx > 0; idx--)
9729 {
9730 if (*t == NUL) /* EOL reached */
9731 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009732 if (enc_utf8 && comp)
9733 t += utf_ptr2len(t);
9734 else
9735 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009736 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009737 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009738#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009739 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009741#endif
9742}
9743
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009744/*
9745 * "byteidx()" function
9746 */
9747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009749{
9750 byteidx(argvars, rettv, FALSE);
9751}
9752
9753/*
9754 * "byteidxcomp()" function
9755 */
9756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009757f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009758{
9759 byteidx(argvars, rettv, TRUE);
9760}
9761
Bram Moolenaardb913952012-06-29 12:54:53 +02009762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009763func_call(
9764 char_u *name,
9765 typval_T *args,
9766 dict_T *selfdict,
9767 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009768{
9769 listitem_T *item;
9770 typval_T argv[MAX_FUNC_ARGS + 1];
9771 int argc = 0;
9772 int dummy;
9773 int r = 0;
9774
9775 for (item = args->vval.v_list->lv_first; item != NULL;
9776 item = item->li_next)
9777 {
9778 if (argc == MAX_FUNC_ARGS)
9779 {
9780 EMSG(_("E699: Too many arguments"));
9781 break;
9782 }
9783 /* Make a copy of each argument. This is needed to be able to set
9784 * v_lock to VAR_FIXED in the copy without changing the original list.
9785 */
9786 copy_tv(&item->li_tv, &argv[argc++]);
9787 }
9788
9789 if (item == NULL)
9790 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9791 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9792 &dummy, TRUE, selfdict);
9793
9794 /* Free the arguments. */
9795 while (argc > 0)
9796 clear_tv(&argv[--argc]);
9797
9798 return r;
9799}
9800
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009801/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009802 * "call(func, arglist)" function
9803 */
9804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009805f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009806{
9807 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009808 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009809
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009810 if (argvars[1].v_type != VAR_LIST)
9811 {
9812 EMSG(_(e_listreq));
9813 return;
9814 }
9815 if (argvars[1].vval.v_list == NULL)
9816 return;
9817
9818 if (argvars[0].v_type == VAR_FUNC)
9819 func = argvars[0].vval.v_string;
9820 else
9821 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009822 if (*func == NUL)
9823 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009824
Bram Moolenaare9a41262005-01-15 22:18:47 +00009825 if (argvars[2].v_type != VAR_UNKNOWN)
9826 {
9827 if (argvars[2].v_type != VAR_DICT)
9828 {
9829 EMSG(_(e_dictreq));
9830 return;
9831 }
9832 selfdict = argvars[2].vval.v_dict;
9833 }
9834
Bram Moolenaardb913952012-06-29 12:54:53 +02009835 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009836}
9837
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009838#ifdef FEAT_FLOAT
9839/*
9840 * "ceil({float})" function
9841 */
9842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009843f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009844{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009845 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009846
9847 rettv->v_type = VAR_FLOAT;
9848 if (get_float_arg(argvars, &f) == OK)
9849 rettv->vval.v_float = ceil(f);
9850 else
9851 rettv->vval.v_float = 0.0;
9852}
9853#endif
9854
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009855#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009856/*
9857 * Get a callback from "arg". It can be a Funcref or a function name.
9858 * When "arg" is zero return an empty string.
9859 * Return NULL for an invalid argument.
9860 */
9861 static char_u *
9862get_callback(typval_T *arg)
9863{
9864 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9865 return arg->vval.v_string;
9866 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9867 return (char_u *)"";
9868 EMSG(_("E999: Invalid callback argument"));
9869 return NULL;
9870}
9871
9872/*
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009873 * Get the option entries from "dict", and parse them.
9874 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009875 */
9876 static int
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009877get_job_options(dict_T *dict, jobopt_T *opt)
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009878{
9879 dictitem_T *item;
9880 char_u *mode;
9881
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009882 if (dict == NULL)
9883 return OK;
9884
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009885 if ((item = dict_find(dict, (char_u *)"mode", -1)) != NULL)
9886 {
9887 mode = get_tv_string(&item->di_tv);
9888 if (STRCMP(mode, "nl") == 0)
9889 opt->jo_mode = MODE_NL;
9890 else if (STRCMP(mode, "raw") == 0)
9891 opt->jo_mode = MODE_RAW;
9892 else if (STRCMP(mode, "js") == 0)
9893 opt->jo_mode = MODE_JS;
9894 else if (STRCMP(mode, "json") == 0)
9895 opt->jo_mode = MODE_JSON;
9896 else
9897 {
9898 EMSG2(_(e_invarg2), mode);
9899 return FAIL;
9900 }
9901 }
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009902
9903 if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
9904 {
9905 opt->jo_callback = get_callback(&item->di_tv);
9906 if (opt->jo_callback == NULL)
9907 {
9908 EMSG2(_(e_invarg2), "callback");
9909 return FAIL;
9910 }
9911 }
9912
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009913 return OK;
9914}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009915#endif
9916
9917#ifdef FEAT_CHANNEL
9918/*
9919 * Get the channel from the argument.
9920 * Returns NULL if the handle is invalid.
9921 */
9922 static channel_T *
9923get_channel_arg(typval_T *tv)
9924{
9925 channel_T *channel;
9926
9927 if (tv->v_type != VAR_CHANNEL)
9928 {
9929 EMSG2(_(e_invarg2), get_tv_string(tv));
9930 return NULL;
9931 }
9932 channel = tv->vval.v_channel;
9933
9934 if (channel == NULL || !channel_is_open(channel))
9935 {
9936 EMSG(_("E906: not an open channel"));
9937 return NULL;
9938 }
9939 return channel;
9940}
9941
9942/*
9943 * "ch_close()" function
9944 */
9945 static void
9946f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9947{
9948 channel_T *channel = get_channel_arg(&argvars[0]);
9949
9950 if (channel != NULL)
9951 channel_close(channel);
9952}
9953
9954/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009955 * "ch_log()" function
9956 */
9957 static void
9958f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
9959{
9960 char_u *msg = get_tv_string(&argvars[0]);
9961 channel_T *channel = NULL;
9962
9963 if (argvars[1].v_type != VAR_UNKNOWN)
9964 channel = get_channel_arg(&argvars[1]);
9965
9966 ch_log(channel, (char *)msg);
9967}
9968
9969/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009970 * "ch_logfile()" function
9971 */
9972 static void
9973f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9974{
9975 char_u *fname;
9976 char_u *opt = (char_u *)"";
9977 char_u buf[NUMBUFLEN];
9978 FILE *file = NULL;
9979
9980 fname = get_tv_string(&argvars[0]);
9981 if (argvars[1].v_type == VAR_STRING)
9982 opt = get_tv_string_buf(&argvars[1], buf);
9983 if (*fname != NUL)
9984 {
9985 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
9986 if (file == NULL)
9987 {
9988 EMSG2(_(e_notopen), fname);
9989 return;
9990 }
9991 }
9992 ch_logfile(file);
9993}
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009994
9995/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009996 * "ch_open()" function
9997 */
9998 static void
9999f_ch_open(typval_T *argvars, typval_T *rettv)
10000{
10001 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010002 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010003 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010004 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010005 int waittime = 0;
10006 int timeout = 2000;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010007 jobopt_T options;
Bram Moolenaar77073442016-02-13 23:23:53 +010010008 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010009
10010 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010011 rettv->v_type = VAR_CHANNEL;
10012 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010013
10014 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010015 if (argvars[1].v_type != VAR_UNKNOWN
10016 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010017 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010018 EMSG(_(e_invarg));
10019 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010020 }
10021
10022 /* parse address */
10023 p = vim_strchr(address, ':');
10024 if (p == NULL)
10025 {
10026 EMSG2(_(e_invarg2), address);
10027 return;
10028 }
10029 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010030 port = strtol((char *)p, &rest, 10);
10031 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010032 {
10033 p[-1] = ':';
10034 EMSG2(_(e_invarg2), address);
10035 return;
10036 }
10037
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010038 options.jo_mode = MODE_JSON;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010039 options.jo_callback = NULL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010040 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010041 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010010042 dict_T *dict = argvars[1].vval.v_dict;
10043 dictitem_T *item;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010044
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010010045 /* parse argdict */
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010046 if (get_job_options(dict, &options) == FAIL)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010047 return;
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010010048 if ((item = dict_find(dict, (char_u *)"waittime", -1)) != NULL)
10049 waittime = get_tv_number(&item->di_tv);
10050 if ((item = dict_find(dict, (char_u *)"timeout", -1)) != NULL)
10051 timeout = get_tv_number(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010052 }
Bram Moolenaare74e8e72016-02-16 22:01:30 +010010053 if (timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010054 {
10055 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010056 return;
10057 }
10058
Bram Moolenaar77073442016-02-13 23:23:53 +010010059 channel = channel_open((char *)address, port, waittime, NULL);
10060 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010061 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010062 rettv->vval.v_channel = channel;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010063 channel_set_options(channel, &options);
Bram Moolenaar77073442016-02-13 23:23:53 +010010064 channel_set_timeout(channel, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010065 }
10066}
10067
10068/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010069 * "ch_readraw()" function
10070 */
10071 static void
10072f_ch_readraw(typval_T *argvars, typval_T *rettv)
10073{
Bram Moolenaar77073442016-02-13 23:23:53 +010010074 channel_T *channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010075
10076 /* return an empty string by default */
10077 rettv->v_type = VAR_STRING;
10078 rettv->vval.v_string = NULL;
10079
Bram Moolenaar77073442016-02-13 23:23:53 +010010080 channel = get_channel_arg(&argvars[0]);
10081 if (channel != NULL)
10082 rettv->vval.v_string = channel_read_block(channel);
10083}
10084
10085/*
10086 * "ch_status()" function
10087 */
10088 static void
10089f_ch_status(typval_T *argvars, typval_T *rettv)
10090{
10091 /* return an empty string by default */
10092 rettv->v_type = VAR_STRING;
10093
10094 if (argvars[0].v_type != VAR_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010095 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010096 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10097 rettv->vval.v_string = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010098 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010099 else
10100 rettv->vval.v_string = vim_strsave(
10101 (char_u *)channel_status(argvars[0].vval.v_channel));
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010102}
10103
10104/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010105 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010106 * Returns the channel if the caller should read the response.
10107 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010108 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010109 static channel_T *
Bram Moolenaara07fec92016-02-05 21:04:08 +010010110send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010111{
Bram Moolenaar77073442016-02-13 23:23:53 +010010112 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010113 char_u *callback = NULL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010114 jobopt_T options;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010115
Bram Moolenaar77073442016-02-13 23:23:53 +010010116 channel = get_channel_arg(&argvars[0]);
10117 if (channel == NULL)
10118 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010119
10120 if (argvars[2].v_type != VAR_UNKNOWN)
10121 {
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010122 if (argvars[2].v_type != VAR_DICT)
10123 {
10124 EMSG(_(e_invarg));
Bram Moolenaar77073442016-02-13 23:23:53 +010010125 return NULL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010126 }
10127 options.jo_callback = NULL;
10128 if (get_job_options(argvars[2].vval.v_dict, &options) == FAIL)
10129 return NULL;
10130 callback = options.jo_callback;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010131 }
Bram Moolenaara07fec92016-02-05 21:04:08 +010010132 /* Set the callback. An empty callback means no callback and not reading
10133 * the response. */
10134 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010135 channel_set_req_callback(channel, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010136
Bram Moolenaar77073442016-02-13 23:23:53 +010010137 if (channel_send(channel, text, fun) == OK && callback == NULL)
10138 return channel;
10139 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010140}
10141
10142/*
10143 * "ch_sendexpr()" function
10144 */
10145 static void
10146f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10147{
10148 char_u *text;
10149 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010150 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010151 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010152 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010153
10154 /* return an empty string by default */
10155 rettv->v_type = VAR_STRING;
10156 rettv->vval.v_string = NULL;
10157
Bram Moolenaar77073442016-02-13 23:23:53 +010010158 channel = get_channel_arg(&argvars[0]);
10159 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010160 return;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010161
Bram Moolenaar77073442016-02-13 23:23:53 +010010162 ch_mode = channel_get_mode(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010163 if (ch_mode == MODE_RAW)
10164 {
10165 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10166 return;
10167 }
10168
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010169 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010170 text = json_encode_nr_expr(id, &argvars[1],
10171 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010172 if (text == NULL)
10173 return;
10174
Bram Moolenaar77073442016-02-13 23:23:53 +010010175 channel = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010176 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010177 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010178 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010179 if (channel_read_json_block(channel, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010180 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010181 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010182
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010183 /* Move the item from the list and then change the type to
10184 * avoid the value being freed. */
10185 *rettv = list->lv_last->li_tv;
10186 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010187 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010188 }
10189 }
10190}
10191
10192/*
10193 * "ch_sendraw()" function
10194 */
10195 static void
10196f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10197{
10198 char_u buf[NUMBUFLEN];
10199 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010200 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010201
10202 /* return an empty string by default */
10203 rettv->v_type = VAR_STRING;
10204 rettv->vval.v_string = NULL;
10205
10206 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar77073442016-02-13 23:23:53 +010010207 channel = send_common(argvars, text, 0, "sendraw");
10208 if (channel != NULL)
10209 rettv->vval.v_string = channel_read_block(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010210}
10211#endif
10212
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010213/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010214 * "changenr()" function
10215 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010217f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010218{
10219 rettv->vval.v_number = curbuf->b_u_seq_cur;
10220}
10221
10222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 * "char2nr(string)" function
10224 */
10225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010226f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227{
10228#ifdef FEAT_MBYTE
10229 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010230 {
10231 int utf8 = 0;
10232
10233 if (argvars[1].v_type != VAR_UNKNOWN)
10234 utf8 = get_tv_number_chk(&argvars[1], NULL);
10235
10236 if (utf8)
10237 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10238 else
10239 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 else
10242#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010243 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244}
10245
10246/*
10247 * "cindent(lnum)" function
10248 */
10249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010250f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251{
10252#ifdef FEAT_CINDENT
10253 pos_T pos;
10254 linenr_T lnum;
10255
10256 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10259 {
10260 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010261 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 curwin->w_cursor = pos;
10263 }
10264 else
10265#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010266 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267}
10268
10269/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010270 * "clearmatches()" function
10271 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010273f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010274{
10275#ifdef FEAT_SEARCH_EXTRA
10276 clear_matches(curwin);
10277#endif
10278}
10279
10280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 * "col(string)" function
10282 */
10283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010284f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285{
10286 colnr_T col = 0;
10287 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010288 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010290 fp = var2fpos(&argvars[0], FALSE, &fnum);
10291 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 {
10293 if (fp->col == MAXCOL)
10294 {
10295 /* '> can be MAXCOL, get the length of the line then */
10296 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010297 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 else
10299 col = MAXCOL;
10300 }
10301 else
10302 {
10303 col = fp->col + 1;
10304#ifdef FEAT_VIRTUALEDIT
10305 /* col(".") when the cursor is on the NUL at the end of the line
10306 * because of "coladd" can be seen as an extra column. */
10307 if (virtual_active() && fp == &curwin->w_cursor)
10308 {
10309 char_u *p = ml_get_cursor();
10310
10311 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10312 curwin->w_virtcol - curwin->w_cursor.coladd))
10313 {
10314# ifdef FEAT_MBYTE
10315 int l;
10316
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010317 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 col += l;
10319# else
10320 if (*p != NUL && p[1] == NUL)
10321 ++col;
10322# endif
10323 }
10324 }
10325#endif
10326 }
10327 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329}
10330
Bram Moolenaar572cb562005-08-05 21:35:02 +000010331#if defined(FEAT_INS_EXPAND)
10332/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010333 * "complete()" function
10334 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010336f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010337{
10338 int startcol;
10339
10340 if ((State & INSERT) == 0)
10341 {
10342 EMSG(_("E785: complete() can only be used in Insert mode"));
10343 return;
10344 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010345
10346 /* Check for undo allowed here, because if something was already inserted
10347 * the line was already saved for undo and this check isn't done. */
10348 if (!undo_allowed())
10349 return;
10350
Bram Moolenaarade00832006-03-10 21:46:58 +000010351 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10352 {
10353 EMSG(_(e_invarg));
10354 return;
10355 }
10356
10357 startcol = get_tv_number_chk(&argvars[0], NULL);
10358 if (startcol <= 0)
10359 return;
10360
10361 set_completion(startcol - 1, argvars[1].vval.v_list);
10362}
10363
10364/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010365 * "complete_add()" function
10366 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010368f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010369{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010370 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010371}
10372
10373/*
10374 * "complete_check()" function
10375 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010377f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010378{
10379 int saved = RedrawingDisabled;
10380
10381 RedrawingDisabled = 0;
10382 ins_compl_check_keys(0);
10383 rettv->vval.v_number = compl_interrupted;
10384 RedrawingDisabled = saved;
10385}
10386#endif
10387
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388/*
10389 * "confirm(message, buttons[, default [, type]])" function
10390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010392f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393{
10394#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10395 char_u *message;
10396 char_u *buttons = NULL;
10397 char_u buf[NUMBUFLEN];
10398 char_u buf2[NUMBUFLEN];
10399 int def = 1;
10400 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010401 char_u *typestr;
10402 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010404 message = get_tv_string_chk(&argvars[0]);
10405 if (message == NULL)
10406 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010407 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010409 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10410 if (buttons == NULL)
10411 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010412 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010414 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010415 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10418 if (typestr == NULL)
10419 error = TRUE;
10420 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010422 switch (TOUPPER_ASC(*typestr))
10423 {
10424 case 'E': type = VIM_ERROR; break;
10425 case 'Q': type = VIM_QUESTION; break;
10426 case 'I': type = VIM_INFO; break;
10427 case 'W': type = VIM_WARNING; break;
10428 case 'G': type = VIM_GENERIC; break;
10429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 }
10431 }
10432 }
10433 }
10434
10435 if (buttons == NULL || *buttons == NUL)
10436 buttons = (char_u *)_("&Ok");
10437
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010438 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010440 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441#endif
10442}
10443
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010444/*
10445 * "copy()" function
10446 */
10447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010448f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010449{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010450 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010451}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010453#ifdef FEAT_FLOAT
10454/*
10455 * "cos()" function
10456 */
10457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010458f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010459{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010460 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010461
10462 rettv->v_type = VAR_FLOAT;
10463 if (get_float_arg(argvars, &f) == OK)
10464 rettv->vval.v_float = cos(f);
10465 else
10466 rettv->vval.v_float = 0.0;
10467}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010468
10469/*
10470 * "cosh()" function
10471 */
10472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010473f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010474{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010475 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010476
10477 rettv->v_type = VAR_FLOAT;
10478 if (get_float_arg(argvars, &f) == OK)
10479 rettv->vval.v_float = cosh(f);
10480 else
10481 rettv->vval.v_float = 0.0;
10482}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010483#endif
10484
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010486 * "count()" function
10487 */
10488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010489f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010490{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010491 long n = 0;
10492 int ic = FALSE;
10493
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010495 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 listitem_T *li;
10497 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010498 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010499
Bram Moolenaare9a41262005-01-15 22:18:47 +000010500 if ((l = argvars[0].vval.v_list) != NULL)
10501 {
10502 li = l->lv_first;
10503 if (argvars[2].v_type != VAR_UNKNOWN)
10504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 int error = FALSE;
10506
10507 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010508 if (argvars[3].v_type != VAR_UNKNOWN)
10509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 idx = get_tv_number_chk(&argvars[3], &error);
10511 if (!error)
10512 {
10513 li = list_find(l, idx);
10514 if (li == NULL)
10515 EMSGN(_(e_listidx), idx);
10516 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010517 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010518 if (error)
10519 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010520 }
10521
10522 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010523 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010524 ++n;
10525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010526 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010527 else if (argvars[0].v_type == VAR_DICT)
10528 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010529 int todo;
10530 dict_T *d;
10531 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010532
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010533 if ((d = argvars[0].vval.v_dict) != NULL)
10534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 int error = FALSE;
10536
Bram Moolenaare9a41262005-01-15 22:18:47 +000010537 if (argvars[2].v_type != VAR_UNKNOWN)
10538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010539 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010540 if (argvars[3].v_type != VAR_UNKNOWN)
10541 EMSG(_(e_invarg));
10542 }
10543
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010544 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010545 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010546 {
10547 if (!HASHITEM_EMPTY(hi))
10548 {
10549 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010550 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010551 ++n;
10552 }
10553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010554 }
10555 }
10556 else
10557 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010558 rettv->vval.v_number = n;
10559}
10560
10561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10563 *
10564 * Checks the existence of a cscope connection.
10565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010567f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
10569#ifdef FEAT_CSCOPE
10570 int num = 0;
10571 char_u *dbpath = NULL;
10572 char_u *prepend = NULL;
10573 char_u buf[NUMBUFLEN];
10574
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010575 if (argvars[0].v_type != VAR_UNKNOWN
10576 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578 num = (int)get_tv_number(&argvars[0]);
10579 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010580 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010581 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 }
10583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585#endif
10586}
10587
10588/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010589 * "cursor(lnum, col)" function, or
10590 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010592 * Moves the cursor to the specified line and column.
10593 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010596f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597{
10598 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010599#ifdef FEAT_VIRTUALEDIT
10600 long coladd = 0;
10601#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010602 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010604 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010605 if (argvars[1].v_type == VAR_UNKNOWN)
10606 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010607 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010608 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010609
Bram Moolenaar493c1782014-05-28 14:34:46 +020010610 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010611 {
10612 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010613 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010614 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010615 line = pos.lnum;
10616 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010617#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010618 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010619#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010620 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010621 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010622 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010623 set_curswant = FALSE;
10624 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010625 }
10626 else
10627 {
10628 line = get_tv_lnum(argvars);
10629 col = get_tv_number_chk(&argvars[1], NULL);
10630#ifdef FEAT_VIRTUALEDIT
10631 if (argvars[2].v_type != VAR_UNKNOWN)
10632 coladd = get_tv_number_chk(&argvars[2], NULL);
10633#endif
10634 }
10635 if (line < 0 || col < 0
10636#ifdef FEAT_VIRTUALEDIT
10637 || coladd < 0
10638#endif
10639 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 if (line > 0)
10642 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 if (col > 0)
10644 curwin->w_cursor.col = col - 1;
10645#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010646 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647#endif
10648
10649 /* Make sure the cursor is in a valid position. */
10650 check_cursor();
10651#ifdef FEAT_MBYTE
10652 /* Correct cursor for multi-byte character. */
10653 if (has_mbyte)
10654 mb_adjust_cursor();
10655#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010656
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010657 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010658 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659}
10660
10661/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010662 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 */
10664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010665f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010667 int noref = 0;
10668
10669 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010670 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010671 if (noref < 0 || noref > 1)
10672 EMSG(_(e_invarg));
10673 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010674 {
10675 current_copyID += COPYID_INC;
10676 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678}
10679
10680/*
10681 * "delete()" function
10682 */
10683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010684f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010686 char_u nbuf[NUMBUFLEN];
10687 char_u *name;
10688 char_u *flags;
10689
10690 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010692 return;
10693
10694 name = get_tv_string(&argvars[0]);
10695 if (name == NULL || *name == NUL)
10696 {
10697 EMSG(_(e_invarg));
10698 return;
10699 }
10700
10701 if (argvars[1].v_type != VAR_UNKNOWN)
10702 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010704 flags = (char_u *)"";
10705
10706 if (*flags == NUL)
10707 /* delete a file */
10708 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10709 else if (STRCMP(flags, "d") == 0)
10710 /* delete an empty directory */
10711 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10712 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010713 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010714 rettv->vval.v_number = delete_recursive(name);
10715 else
10716 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717}
10718
10719/*
10720 * "did_filetype()" function
10721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010723f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724{
10725#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727#endif
10728}
10729
10730/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010731 * "diff_filler()" function
10732 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010734f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010735{
10736#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010738#endif
10739}
10740
10741/*
10742 * "diff_hlID()" function
10743 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010745f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010746{
10747#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010749 static linenr_T prev_lnum = 0;
10750 static int changedtick = 0;
10751 static int fnum = 0;
10752 static int change_start = 0;
10753 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010754 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010755 int filler_lines;
10756 int col;
10757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010758 if (lnum < 0) /* ignore type error in {lnum} arg */
10759 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010760 if (lnum != prev_lnum
10761 || changedtick != curbuf->b_changedtick
10762 || fnum != curbuf->b_fnum)
10763 {
10764 /* New line, buffer, change: need to get the values. */
10765 filler_lines = diff_check(curwin, lnum);
10766 if (filler_lines < 0)
10767 {
10768 if (filler_lines == -1)
10769 {
10770 change_start = MAXCOL;
10771 change_end = -1;
10772 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10773 hlID = HLF_ADD; /* added line */
10774 else
10775 hlID = HLF_CHD; /* changed line */
10776 }
10777 else
10778 hlID = HLF_ADD; /* added line */
10779 }
10780 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010781 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010782 prev_lnum = lnum;
10783 changedtick = curbuf->b_changedtick;
10784 fnum = curbuf->b_fnum;
10785 }
10786
10787 if (hlID == HLF_CHD || hlID == HLF_TXD)
10788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010789 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010790 if (col >= change_start && col <= change_end)
10791 hlID = HLF_TXD; /* changed text */
10792 else
10793 hlID = HLF_CHD; /* changed line */
10794 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010795 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010796#endif
10797}
10798
10799/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010800 * "disable_char_avail_for_testing({expr})" function
10801 */
10802 static void
10803f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10804{
10805 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10806}
10807
10808/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010809 * "empty({expr})" function
10810 */
10811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010812f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010813{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010814 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010815
10816 switch (argvars[0].v_type)
10817 {
10818 case VAR_STRING:
10819 case VAR_FUNC:
10820 n = argvars[0].vval.v_string == NULL
10821 || *argvars[0].vval.v_string == NUL;
10822 break;
10823 case VAR_NUMBER:
10824 n = argvars[0].vval.v_number == 0;
10825 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010826 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010827#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010828 n = argvars[0].vval.v_float == 0.0;
10829 break;
10830#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010831 case VAR_LIST:
10832 n = argvars[0].vval.v_list == NULL
10833 || argvars[0].vval.v_list->lv_first == NULL;
10834 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835 case VAR_DICT:
10836 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010837 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010839 case VAR_SPECIAL:
10840 n = argvars[0].vval.v_number != VVAL_TRUE;
10841 break;
10842
Bram Moolenaar835dc632016-02-07 14:27:38 +010010843 case VAR_JOB:
10844#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010845 n = argvars[0].vval.v_job == NULL
10846 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10847 break;
10848#endif
10849 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010010850#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010851 n = argvars[0].vval.v_channel == NULL
10852 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010853 break;
10854#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010855 case VAR_UNKNOWN:
10856 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10857 n = TRUE;
10858 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010859 }
10860
10861 rettv->vval.v_number = n;
10862}
10863
10864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 * "escape({string}, {chars})" function
10866 */
10867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010868f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869{
10870 char_u buf[NUMBUFLEN];
10871
Bram Moolenaar758711c2005-02-02 23:11:38 +000010872 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10873 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875}
10876
10877/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010878 * "eval()" function
10879 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010881f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010882{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010883 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010885 s = get_tv_string_chk(&argvars[0]);
10886 if (s != NULL)
10887 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010888
Bram Moolenaar615b9972015-01-14 17:15:05 +010010889 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010890 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10891 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010892 if (p != NULL && !aborting())
10893 EMSG2(_(e_invexpr2), p);
10894 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010895 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010896 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010898 else if (*s != NUL)
10899 EMSG(_(e_trailing));
10900}
10901
10902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 * "eventhandler()" function
10904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010906f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909}
10910
10911/*
10912 * "executable()" function
10913 */
10914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010915f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010917 char_u *name = get_tv_string(&argvars[0]);
10918
10919 /* Check in $PATH and also check directly if there is a directory name. */
10920 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10921 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010922}
10923
10924/*
10925 * "exepath()" function
10926 */
10927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010928f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010929{
10930 char_u *p = NULL;
10931
Bram Moolenaarb5971142015-03-21 17:32:19 +010010932 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010933 rettv->v_type = VAR_STRING;
10934 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935}
10936
10937/*
10938 * "exists()" function
10939 */
10940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010941f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942{
10943 char_u *p;
10944 char_u *name;
10945 int n = FALSE;
10946 int len = 0;
10947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 if (*p == '$') /* environment variable */
10950 {
10951 /* first try "normal" environment variables (fast) */
10952 if (mch_getenv(p + 1) != NULL)
10953 n = TRUE;
10954 else
10955 {
10956 /* try expanding things like $VIM and ${HOME} */
10957 p = expand_env_save(p);
10958 if (p != NULL && *p != '$')
10959 n = TRUE;
10960 vim_free(p);
10961 }
10962 }
10963 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010966 if (*skipwhite(p) != NUL)
10967 n = FALSE; /* trailing garbage */
10968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 else if (*p == '*') /* internal or user defined function */
10970 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010971 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 }
10973 else if (*p == ':')
10974 {
10975 n = cmd_exists(p + 1);
10976 }
10977 else if (*p == '#')
10978 {
10979#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010980 if (p[1] == '#')
10981 n = autocmd_supported(p + 2);
10982 else
10983 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984#endif
10985 }
10986 else /* internal variable */
10987 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010988 char_u *tofree;
10989 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010991 /* get_name_len() takes care of expanding curly braces */
10992 name = p;
10993 len = get_name_len(&p, &tofree, TRUE, FALSE);
10994 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010996 if (tofree != NULL)
10997 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010998 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010999 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011001 /* handle d.key, l[idx], f(expr) */
11002 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11003 if (n)
11004 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 }
11006 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011007 if (*p != NUL)
11008 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011010 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 }
11012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014}
11015
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011016#ifdef FEAT_FLOAT
11017/*
11018 * "exp()" function
11019 */
11020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011021f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011022{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011023 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011024
11025 rettv->v_type = VAR_FLOAT;
11026 if (get_float_arg(argvars, &f) == OK)
11027 rettv->vval.v_float = exp(f);
11028 else
11029 rettv->vval.v_float = 0.0;
11030}
11031#endif
11032
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033/*
11034 * "expand()" function
11035 */
11036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011037f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038{
11039 char_u *s;
11040 int len;
11041 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011042 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011044 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011045 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011048 if (argvars[1].v_type != VAR_UNKNOWN
11049 && argvars[2].v_type != VAR_UNKNOWN
11050 && get_tv_number_chk(&argvars[2], &error)
11051 && !error)
11052 {
11053 rettv->v_type = VAR_LIST;
11054 rettv->vval.v_list = NULL;
11055 }
11056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 if (*s == '%' || *s == '#' || *s == '<')
11059 {
11060 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011061 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011063 if (rettv->v_type == VAR_LIST)
11064 {
11065 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11066 list_append_string(rettv->vval.v_list, result, -1);
11067 else
11068 vim_free(result);
11069 }
11070 else
11071 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 }
11073 else
11074 {
11075 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011076 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 if (argvars[1].v_type != VAR_UNKNOWN
11078 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011079 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011080 if (!error)
11081 {
11082 ExpandInit(&xpc);
11083 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011084 if (p_wic)
11085 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011086 if (rettv->v_type == VAR_STRING)
11087 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11088 options, WILD_ALL);
11089 else if (rettv_list_alloc(rettv) != FAIL)
11090 {
11091 int i;
11092
11093 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11094 for (i = 0; i < xpc.xp_numfiles; i++)
11095 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11096 ExpandCleanup(&xpc);
11097 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011098 }
11099 else
11100 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 }
11102}
11103
11104/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011105 * Go over all entries in "d2" and add them to "d1".
11106 * When "action" is "error" then a duplicate key is an error.
11107 * When "action" is "force" then a duplicate key is overwritten.
11108 * Otherwise duplicate keys are ignored ("action" is "keep").
11109 */
11110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011111dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011112{
11113 dictitem_T *di1;
11114 hashitem_T *hi2;
11115 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011116 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011117
11118 todo = (int)d2->dv_hashtab.ht_used;
11119 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11120 {
11121 if (!HASHITEM_EMPTY(hi2))
11122 {
11123 --todo;
11124 di1 = dict_find(d1, hi2->hi_key, -1);
11125 if (d1->dv_scope != 0)
11126 {
11127 /* Disallow replacing a builtin function in l: and g:.
11128 * Check the key to be valid when adding to any
11129 * scope. */
11130 if (d1->dv_scope == VAR_DEF_SCOPE
11131 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11132 && var_check_func_name(hi2->hi_key,
11133 di1 == NULL))
11134 break;
11135 if (!valid_varname(hi2->hi_key))
11136 break;
11137 }
11138 if (di1 == NULL)
11139 {
11140 di1 = dictitem_copy(HI2DI(hi2));
11141 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11142 dictitem_free(di1);
11143 }
11144 else if (*action == 'e')
11145 {
11146 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11147 break;
11148 }
11149 else if (*action == 'f' && HI2DI(hi2) != di1)
11150 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011151 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11152 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011153 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011154 clear_tv(&di1->di_tv);
11155 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11156 }
11157 }
11158 }
11159}
11160
11161/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011162 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011163 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011164 */
11165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011166f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011167{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011168 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011169
Bram Moolenaare9a41262005-01-15 22:18:47 +000011170 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011171 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011172 list_T *l1, *l2;
11173 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011174 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011176
Bram Moolenaare9a41262005-01-15 22:18:47 +000011177 l1 = argvars[0].vval.v_list;
11178 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011179 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011180 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011181 {
11182 if (argvars[2].v_type != VAR_UNKNOWN)
11183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 before = get_tv_number_chk(&argvars[2], &error);
11185 if (error)
11186 return; /* type error; errmsg already given */
11187
Bram Moolenaar758711c2005-02-02 23:11:38 +000011188 if (before == l1->lv_len)
11189 item = NULL;
11190 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011191 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011192 item = list_find(l1, before);
11193 if (item == NULL)
11194 {
11195 EMSGN(_(e_listidx), before);
11196 return;
11197 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011198 }
11199 }
11200 else
11201 item = NULL;
11202 list_extend(l1, l2, item);
11203
Bram Moolenaare9a41262005-01-15 22:18:47 +000011204 copy_tv(&argvars[0], rettv);
11205 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011207 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11208 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011209 dict_T *d1, *d2;
11210 char_u *action;
11211 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011212
11213 d1 = argvars[0].vval.v_dict;
11214 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011215 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011216 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011217 {
11218 /* Check the third argument. */
11219 if (argvars[2].v_type != VAR_UNKNOWN)
11220 {
11221 static char *(av[]) = {"keep", "force", "error"};
11222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011223 action = get_tv_string_chk(&argvars[2]);
11224 if (action == NULL)
11225 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011226 for (i = 0; i < 3; ++i)
11227 if (STRCMP(action, av[i]) == 0)
11228 break;
11229 if (i == 3)
11230 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011231 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011232 return;
11233 }
11234 }
11235 else
11236 action = (char_u *)"force";
11237
Bram Moolenaara9922d62013-05-30 13:01:18 +020011238 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239
Bram Moolenaare9a41262005-01-15 22:18:47 +000011240 copy_tv(&argvars[0], rettv);
11241 }
11242 }
11243 else
11244 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011245}
11246
11247/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011248 * "feedkeys()" function
11249 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011251f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011252{
11253 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011254 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011255 char_u *keys, *flags;
11256 char_u nbuf[NUMBUFLEN];
11257 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011258 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011259 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011260
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011261 /* This is not allowed in the sandbox. If the commands would still be
11262 * executed in the sandbox it would be OK, but it probably happens later,
11263 * when "sandbox" is no longer set. */
11264 if (check_secure())
11265 return;
11266
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011267 keys = get_tv_string(&argvars[0]);
11268 if (*keys != NUL)
11269 {
11270 if (argvars[1].v_type != VAR_UNKNOWN)
11271 {
11272 flags = get_tv_string_buf(&argvars[1], nbuf);
11273 for ( ; *flags != NUL; ++flags)
11274 {
11275 switch (*flags)
11276 {
11277 case 'n': remap = FALSE; break;
11278 case 'm': remap = TRUE; break;
11279 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011280 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011281 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011282 }
11283 }
11284 }
11285
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011286 /* Need to escape K_SPECIAL and CSI before putting the string in the
11287 * typeahead buffer. */
11288 keys_esc = vim_strsave_escape_csi(keys);
11289 if (keys_esc != NULL)
11290 {
11291 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011292 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011293 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011294 if (vgetc_busy)
11295 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011296 if (execute)
11297 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011298 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011299 }
11300}
11301
11302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 * "filereadable()" function
11304 */
11305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011306f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011308 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 char_u *p;
11310 int n;
11311
Bram Moolenaarc236c162008-07-13 17:41:49 +000011312#ifndef O_NONBLOCK
11313# define O_NONBLOCK 0
11314#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011316 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11317 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 {
11319 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011320 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 }
11322 else
11323 n = FALSE;
11324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011325 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326}
11327
11328/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011329 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 * rights to write into.
11331 */
11332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011333f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011335 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336}
11337
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011338 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011339findfilendir(
11340 typval_T *argvars UNUSED,
11341 typval_T *rettv,
11342 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011343{
11344#ifdef FEAT_SEARCHPATH
11345 char_u *fname;
11346 char_u *fresult = NULL;
11347 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11348 char_u *p;
11349 char_u pathbuf[NUMBUFLEN];
11350 int count = 1;
11351 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011352 int error = FALSE;
11353#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011354
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011355 rettv->vval.v_string = NULL;
11356 rettv->v_type = VAR_STRING;
11357
11358#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011360
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011361 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011363 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11364 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011365 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366 else
11367 {
11368 if (*p != NUL)
11369 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011372 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011374 }
11375
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011376 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11377 error = TRUE;
11378
11379 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 do
11382 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011383 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011384 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011385 fresult = find_file_in_path_option(first ? fname : NULL,
11386 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011387 0, first, path,
11388 find_what,
11389 curbuf->b_ffname,
11390 find_what == FINDFILE_DIR
11391 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011392 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011393
11394 if (fresult != NULL && rettv->v_type == VAR_LIST)
11395 list_append_string(rettv->vval.v_list, fresult, -1);
11396
11397 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011398 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011399
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011400 if (rettv->v_type == VAR_STRING)
11401 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011402#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011403}
11404
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011405static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11406static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011407
11408/*
11409 * Implementation of map() and filter().
11410 */
11411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011412filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011413{
11414 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011415 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011416 listitem_T *li, *nli;
11417 list_T *l = NULL;
11418 dictitem_T *di;
11419 hashtab_T *ht;
11420 hashitem_T *hi;
11421 dict_T *d = NULL;
11422 typval_T save_val;
11423 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011424 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011425 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011426 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011427 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011428 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011429 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011430 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011431
Bram Moolenaare9a41262005-01-15 22:18:47 +000011432 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011433 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011434 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011435 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011436 return;
11437 }
11438 else if (argvars[0].v_type == VAR_DICT)
11439 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011440 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011441 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011442 return;
11443 }
11444 else
11445 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011446 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011447 return;
11448 }
11449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011450 expr = get_tv_string_buf_chk(&argvars[1], buf);
11451 /* On type errors, the preceding call has already displayed an error
11452 * message. Avoid a misleading error message for an empty string that
11453 * was not passed as argument. */
11454 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011456 prepare_vimvar(VV_VAL, &save_val);
11457 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011458
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011459 /* We reset "did_emsg" to be able to detect whether an error
11460 * occurred during evaluation of the expression. */
11461 save_did_emsg = did_emsg;
11462 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011463
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011464 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011467 vimvars[VV_KEY].vv_type = VAR_STRING;
11468
11469 ht = &d->dv_hashtab;
11470 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011471 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011472 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011474 if (!HASHITEM_EMPTY(hi))
11475 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011476 int r;
11477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 --todo;
11479 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011480 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011481 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11482 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011483 break;
11484 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011485 r = filter_map_one(&di->di_tv, expr, map, &rem);
11486 clear_tv(&vimvars[VV_KEY].vv_tv);
11487 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011488 break;
11489 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011490 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011491 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11492 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011493 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011494 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011495 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 }
11497 }
11498 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011499 }
11500 else
11501 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011502 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504 for (li = l->lv_first; li != NULL; li = nli)
11505 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011506 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011507 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011508 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011509 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011510 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011511 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011512 break;
11513 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011515 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011516 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011517 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011518
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011519 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011520 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011521
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011522 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011523 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011524
11525 copy_tv(&argvars[0], rettv);
11526}
11527
11528 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011529filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011530{
Bram Moolenaar33570922005-01-25 22:26:29 +000011531 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011532 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011533 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011534
Bram Moolenaar33570922005-01-25 22:26:29 +000011535 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011536 s = expr;
11537 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011538 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011539 if (*s != NUL) /* check for trailing chars after expr */
11540 {
11541 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011542 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011543 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011544 }
11545 if (map)
11546 {
11547 /* map(): replace the list item value */
11548 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011549 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011550 *tv = rettv;
11551 }
11552 else
11553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 int error = FALSE;
11555
Bram Moolenaare9a41262005-01-15 22:18:47 +000011556 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011557 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011558 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 /* On type error, nothing has been removed; return FAIL to stop the
11560 * loop. The error message was given by get_tv_number_chk(). */
11561 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011562 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011564 retval = OK;
11565theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011566 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011567 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011568}
11569
11570/*
11571 * "filter()" function
11572 */
11573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011574f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011575{
11576 filter_map(argvars, rettv, FALSE);
11577}
11578
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011579/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011580 * "finddir({fname}[, {path}[, {count}]])" function
11581 */
11582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011583f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011584{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011585 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011586}
11587
11588/*
11589 * "findfile({fname}[, {path}[, {count}]])" function
11590 */
11591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011592f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011593{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011594 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011595}
11596
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011597#ifdef FEAT_FLOAT
11598/*
11599 * "float2nr({float})" function
11600 */
11601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011602f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011603{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011604 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011605
11606 if (get_float_arg(argvars, &f) == OK)
11607 {
11608 if (f < -0x7fffffff)
11609 rettv->vval.v_number = -0x7fffffff;
11610 else if (f > 0x7fffffff)
11611 rettv->vval.v_number = 0x7fffffff;
11612 else
11613 rettv->vval.v_number = (varnumber_T)f;
11614 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011615}
11616
11617/*
11618 * "floor({float})" function
11619 */
11620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011621f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011622{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011623 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011624
11625 rettv->v_type = VAR_FLOAT;
11626 if (get_float_arg(argvars, &f) == OK)
11627 rettv->vval.v_float = floor(f);
11628 else
11629 rettv->vval.v_float = 0.0;
11630}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011631
11632/*
11633 * "fmod()" function
11634 */
11635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011636f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011637{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011638 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011639
11640 rettv->v_type = VAR_FLOAT;
11641 if (get_float_arg(argvars, &fx) == OK
11642 && get_float_arg(&argvars[1], &fy) == OK)
11643 rettv->vval.v_float = fmod(fx, fy);
11644 else
11645 rettv->vval.v_float = 0.0;
11646}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011647#endif
11648
Bram Moolenaar0d660222005-01-07 21:51:51 +000011649/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011650 * "fnameescape({string})" function
11651 */
11652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011653f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011654{
11655 rettv->vval.v_string = vim_strsave_fnameescape(
11656 get_tv_string(&argvars[0]), FALSE);
11657 rettv->v_type = VAR_STRING;
11658}
11659
11660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 * "fnamemodify({fname}, {mods})" function
11662 */
11663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011664f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665{
11666 char_u *fname;
11667 char_u *mods;
11668 int usedlen = 0;
11669 int len;
11670 char_u *fbuf = NULL;
11671 char_u buf[NUMBUFLEN];
11672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 fname = get_tv_string_chk(&argvars[0]);
11674 mods = get_tv_string_buf_chk(&argvars[1], buf);
11675 if (fname == NULL || mods == NULL)
11676 fname = NULL;
11677 else
11678 {
11679 len = (int)STRLEN(fname);
11680 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011687 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 vim_free(fbuf);
11689}
11690
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011691static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692
11693/*
11694 * "foldclosed()" function
11695 */
11696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011697foldclosed_both(
11698 typval_T *argvars UNUSED,
11699 typval_T *rettv,
11700 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701{
11702#ifdef FEAT_FOLDING
11703 linenr_T lnum;
11704 linenr_T first, last;
11705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011706 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11708 {
11709 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11710 {
11711 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011712 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011714 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 return;
11716 }
11717 }
11718#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011719 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720}
11721
11722/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011723 * "foldclosed()" function
11724 */
11725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011726f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011727{
11728 foldclosed_both(argvars, rettv, FALSE);
11729}
11730
11731/*
11732 * "foldclosedend()" function
11733 */
11734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011735f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011736{
11737 foldclosed_both(argvars, rettv, TRUE);
11738}
11739
11740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741 * "foldlevel()" function
11742 */
11743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011744f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745{
11746#ifdef FEAT_FOLDING
11747 linenr_T lnum;
11748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011749 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753}
11754
11755/*
11756 * "foldtext()" function
11757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011759f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760{
11761#ifdef FEAT_FOLDING
11762 linenr_T lnum;
11763 char_u *s;
11764 char_u *r;
11765 int len;
11766 char *txt;
11767#endif
11768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 rettv->v_type = VAR_STRING;
11770 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011772 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11773 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11774 <= curbuf->b_ml.ml_line_count
11775 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776 {
11777 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011778 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11779 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 {
11781 if (!linewhite(lnum))
11782 break;
11783 ++lnum;
11784 }
11785
11786 /* Find interesting text in this line. */
11787 s = skipwhite(ml_get(lnum));
11788 /* skip C comment-start */
11789 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011790 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011792 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011793 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011794 {
11795 s = skipwhite(ml_get(lnum + 1));
11796 if (*s == '*')
11797 s = skipwhite(s + 1);
11798 }
11799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 txt = _("+-%s%3ld lines: ");
11801 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011802 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 + 20 /* for %3ld */
11804 + STRLEN(s))); /* concatenated */
11805 if (r != NULL)
11806 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011807 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11808 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11809 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 len = (int)STRLEN(r);
11811 STRCAT(r, s);
11812 /* remove 'foldmarker' and 'commentstring' */
11813 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011814 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 }
11816 }
11817#endif
11818}
11819
11820/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011821 * "foldtextresult(lnum)" function
11822 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011824f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011825{
11826#ifdef FEAT_FOLDING
11827 linenr_T lnum;
11828 char_u *text;
11829 char_u buf[51];
11830 foldinfo_T foldinfo;
11831 int fold_count;
11832#endif
11833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011834 rettv->v_type = VAR_STRING;
11835 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011836#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011838 /* treat illegal types and illegal string values for {lnum} the same */
11839 if (lnum < 0)
11840 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011841 fold_count = foldedCount(curwin, lnum, &foldinfo);
11842 if (fold_count > 0)
11843 {
11844 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11845 &foldinfo, buf);
11846 if (text == buf)
11847 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011849 }
11850#endif
11851}
11852
11853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 * "foreground()" function
11855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011857f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859#ifdef FEAT_GUI
11860 if (gui.in_use)
11861 gui_mch_set_foreground();
11862#else
11863# ifdef WIN32
11864 win32_set_foreground();
11865# endif
11866#endif
11867}
11868
11869/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011870 * "function()" function
11871 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011873f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011874{
11875 char_u *s;
11876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011878 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011879 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011880 /* Don't check an autoload name for existence here. */
11881 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011882 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011883 else
11884 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011885 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011886 {
11887 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011888 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011889
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011890 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11891 * also be called from another script. Using trans_function_name()
11892 * would also work, but some plugins depend on the name being
11893 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011894 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011895 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011896 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011897 if (rettv->vval.v_string != NULL)
11898 {
11899 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011900 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011901 }
11902 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011903 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011904 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011906 }
11907}
11908
11909/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011910 * "garbagecollect()" function
11911 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011913f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011914{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011915 /* This is postponed until we are back at the toplevel, because we may be
11916 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11917 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011918
11919 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11920 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011921}
11922
11923/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011924 * "get()" function
11925 */
11926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011927f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011928{
Bram Moolenaar33570922005-01-25 22:26:29 +000011929 listitem_T *li;
11930 list_T *l;
11931 dictitem_T *di;
11932 dict_T *d;
11933 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011934
Bram Moolenaare9a41262005-01-15 22:18:47 +000011935 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011936 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011937 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011939 int error = FALSE;
11940
11941 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11942 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011943 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011944 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011945 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011946 else if (argvars[0].v_type == VAR_DICT)
11947 {
11948 if ((d = argvars[0].vval.v_dict) != NULL)
11949 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011950 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011951 if (di != NULL)
11952 tv = &di->di_tv;
11953 }
11954 }
11955 else
11956 EMSG2(_(e_listdictarg), "get()");
11957
11958 if (tv == NULL)
11959 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011960 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011961 copy_tv(&argvars[2], rettv);
11962 }
11963 else
11964 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011965}
11966
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011967static 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 +000011968
11969/*
11970 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011971 * Return a range (from start to end) of lines in rettv from the specified
11972 * buffer.
11973 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011974 */
11975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011976get_buffer_lines(
11977 buf_T *buf,
11978 linenr_T start,
11979 linenr_T end,
11980 int retlist,
11981 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011982{
11983 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011984
Bram Moolenaar959a1432013-12-14 12:17:38 +010011985 rettv->v_type = VAR_STRING;
11986 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011987 if (retlist && rettv_list_alloc(rettv) == FAIL)
11988 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011989
11990 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11991 return;
11992
11993 if (!retlist)
11994 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011995 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11996 p = ml_get_buf(buf, start, FALSE);
11997 else
11998 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011999 rettv->vval.v_string = vim_strsave(p);
12000 }
12001 else
12002 {
12003 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012004 return;
12005
12006 if (start < 1)
12007 start = 1;
12008 if (end > buf->b_ml.ml_line_count)
12009 end = buf->b_ml.ml_line_count;
12010 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012011 if (list_append_string(rettv->vval.v_list,
12012 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012013 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012014 }
12015}
12016
12017/*
12018 * "getbufline()" function
12019 */
12020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012021f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012022{
12023 linenr_T lnum;
12024 linenr_T end;
12025 buf_T *buf;
12026
12027 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12028 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012029 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012030 --emsg_off;
12031
Bram Moolenaar661b1822005-07-28 22:36:45 +000012032 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012033 if (argvars[2].v_type == VAR_UNKNOWN)
12034 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012035 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012036 end = get_tv_lnum_buf(&argvars[2], buf);
12037
Bram Moolenaar342337a2005-07-21 21:11:17 +000012038 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012039}
12040
Bram Moolenaar0d660222005-01-07 21:51:51 +000012041/*
12042 * "getbufvar()" function
12043 */
12044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012045f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012046{
12047 buf_T *buf;
12048 buf_T *save_curbuf;
12049 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012050 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012051 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012053 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12054 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012055 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012056 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012057
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012058 rettv->v_type = VAR_STRING;
12059 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012060
12061 if (buf != NULL && varname != NULL)
12062 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012063 /* set curbuf to be our buf, temporarily */
12064 save_curbuf = curbuf;
12065 curbuf = buf;
12066
Bram Moolenaar0d660222005-01-07 21:51:51 +000012067 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012068 {
12069 if (get_option_tv(&varname, rettv, TRUE) == OK)
12070 done = TRUE;
12071 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012072 else if (STRCMP(varname, "changedtick") == 0)
12073 {
12074 rettv->v_type = VAR_NUMBER;
12075 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012076 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012077 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012078 else
12079 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012080 /* Look up the variable. */
12081 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12082 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12083 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012084 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012085 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012086 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012087 done = TRUE;
12088 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012089 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012090
12091 /* restore previous notion of curbuf */
12092 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012093 }
12094
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012095 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12096 /* use the default value */
12097 copy_tv(&argvars[2], rettv);
12098
Bram Moolenaar0d660222005-01-07 21:51:51 +000012099 --emsg_off;
12100}
12101
12102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103 * "getchar()" function
12104 */
12105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012106f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107{
12108 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012109 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012111 /* Position the cursor. Needed after a message that ends in a space. */
12112 windgoto(msg_row, msg_col);
12113
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 ++no_mapping;
12115 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012116 for (;;)
12117 {
12118 if (argvars[0].v_type == VAR_UNKNOWN)
12119 /* getchar(): blocking wait. */
12120 n = safe_vgetc();
12121 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12122 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012123 n = vpeekc_any();
12124 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012125 /* illegal argument or getchar(0) and no char avail: return zero */
12126 n = 0;
12127 else
12128 /* getchar(0) and char avail: return char */
12129 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012130
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012131 if (n == K_IGNORE)
12132 continue;
12133 break;
12134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 --no_mapping;
12136 --allow_keys;
12137
Bram Moolenaar219b8702006-11-01 14:32:36 +000012138 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12139 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12140 vimvars[VV_MOUSE_COL].vv_nr = 0;
12141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 if (IS_SPECIAL(n) || mod_mask != 0)
12144 {
12145 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12146 int i = 0;
12147
12148 /* Turn a special key into three bytes, plus modifier. */
12149 if (mod_mask != 0)
12150 {
12151 temp[i++] = K_SPECIAL;
12152 temp[i++] = KS_MODIFIER;
12153 temp[i++] = mod_mask;
12154 }
12155 if (IS_SPECIAL(n))
12156 {
12157 temp[i++] = K_SPECIAL;
12158 temp[i++] = K_SECOND(n);
12159 temp[i++] = K_THIRD(n);
12160 }
12161#ifdef FEAT_MBYTE
12162 else if (has_mbyte)
12163 i += (*mb_char2bytes)(n, temp + i);
12164#endif
12165 else
12166 temp[i++] = n;
12167 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 rettv->v_type = VAR_STRING;
12169 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012170
12171#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012172 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012173 {
12174 int row = mouse_row;
12175 int col = mouse_col;
12176 win_T *win;
12177 linenr_T lnum;
12178# ifdef FEAT_WINDOWS
12179 win_T *wp;
12180# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012181 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012182
12183 if (row >= 0 && col >= 0)
12184 {
12185 /* Find the window at the mouse coordinates and compute the
12186 * text position. */
12187 win = mouse_find_win(&row, &col);
12188 (void)mouse_comp_pos(win, &row, &col, &lnum);
12189# ifdef FEAT_WINDOWS
12190 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012191 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012192# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012193 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012194 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12195 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12196 }
12197 }
12198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 }
12200}
12201
12202/*
12203 * "getcharmod()" function
12204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012206f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012208 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209}
12210
12211/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012212 * "getcharsearch()" function
12213 */
12214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012215f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012216{
12217 if (rettv_dict_alloc(rettv) != FAIL)
12218 {
12219 dict_T *dict = rettv->vval.v_dict;
12220
12221 dict_add_nr_str(dict, "char", 0L, last_csearch());
12222 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12223 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12224 }
12225}
12226
12227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 * "getcmdline()" function
12229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012231f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->v_type = VAR_STRING;
12234 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235}
12236
12237/*
12238 * "getcmdpos()" function
12239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012241f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244}
12245
12246/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012247 * "getcmdtype()" function
12248 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012250f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012251{
12252 rettv->v_type = VAR_STRING;
12253 rettv->vval.v_string = alloc(2);
12254 if (rettv->vval.v_string != NULL)
12255 {
12256 rettv->vval.v_string[0] = get_cmdline_type();
12257 rettv->vval.v_string[1] = NUL;
12258 }
12259}
12260
12261/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012262 * "getcmdwintype()" function
12263 */
12264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012265f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012266{
12267 rettv->v_type = VAR_STRING;
12268 rettv->vval.v_string = NULL;
12269#ifdef FEAT_CMDWIN
12270 rettv->vval.v_string = alloc(2);
12271 if (rettv->vval.v_string != NULL)
12272 {
12273 rettv->vval.v_string[0] = cmdwin_type;
12274 rettv->vval.v_string[1] = NUL;
12275 }
12276#endif
12277}
12278
12279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 * "getcwd()" function
12281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012283f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012285 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012286 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012289 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012290
12291 wp = find_tabwin(&argvars[0], &argvars[1]);
12292 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012294 if (wp->w_localdir != NULL)
12295 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12296 else if(globaldir != NULL)
12297 rettv->vval.v_string = vim_strsave(globaldir);
12298 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012299 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012300 cwd = alloc(MAXPATHL);
12301 if (cwd != NULL)
12302 {
12303 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12304 rettv->vval.v_string = vim_strsave(cwd);
12305 vim_free(cwd);
12306 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012307 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012308#ifdef BACKSLASH_IN_FILENAME
12309 if (rettv->vval.v_string != NULL)
12310 slash_adjust(rettv->vval.v_string);
12311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 }
12313}
12314
12315/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012316 * "getfontname()" function
12317 */
12318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012319f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012320{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321 rettv->v_type = VAR_STRING;
12322 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012323#ifdef FEAT_GUI
12324 if (gui.in_use)
12325 {
12326 GuiFont font;
12327 char_u *name = NULL;
12328
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012329 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012330 {
12331 /* Get the "Normal" font. Either the name saved by
12332 * hl_set_font_name() or from the font ID. */
12333 font = gui.norm_font;
12334 name = hl_get_font_name();
12335 }
12336 else
12337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012339 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12340 return;
12341 font = gui_mch_get_font(name, FALSE);
12342 if (font == NOFONT)
12343 return; /* Invalid font name, return empty string. */
12344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012346 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012347 gui_mch_free_font(font);
12348 }
12349#endif
12350}
12351
12352/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012353 * "getfperm({fname})" function
12354 */
12355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012356f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012357{
12358 char_u *fname;
12359 struct stat st;
12360 char_u *perm = NULL;
12361 char_u flags[] = "rwx";
12362 int i;
12363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012367 if (mch_stat((char *)fname, &st) >= 0)
12368 {
12369 perm = vim_strsave((char_u *)"---------");
12370 if (perm != NULL)
12371 {
12372 for (i = 0; i < 9; i++)
12373 {
12374 if (st.st_mode & (1 << (8 - i)))
12375 perm[i] = flags[i % 3];
12376 }
12377 }
12378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012380}
12381
12382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 * "getfsize({fname})" function
12384 */
12385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012386f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387{
12388 char_u *fname;
12389 struct stat st;
12390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394
12395 if (mch_stat((char *)fname, &st) >= 0)
12396 {
12397 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012401 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012402
12403 /* non-perfect check for overflow */
12404 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12405 rettv->vval.v_number = -2;
12406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 }
12408 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410}
12411
12412/*
12413 * "getftime({fname})" function
12414 */
12415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012416f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417{
12418 char_u *fname;
12419 struct stat st;
12420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012421 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422
12423 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012424 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012426 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427}
12428
12429/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012430 * "getftype({fname})" function
12431 */
12432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012433f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012434{
12435 char_u *fname;
12436 struct stat st;
12437 char_u *type = NULL;
12438 char *t;
12439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012440 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012442 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012443 if (mch_lstat((char *)fname, &st) >= 0)
12444 {
12445#ifdef S_ISREG
12446 if (S_ISREG(st.st_mode))
12447 t = "file";
12448 else if (S_ISDIR(st.st_mode))
12449 t = "dir";
12450# ifdef S_ISLNK
12451 else if (S_ISLNK(st.st_mode))
12452 t = "link";
12453# endif
12454# ifdef S_ISBLK
12455 else if (S_ISBLK(st.st_mode))
12456 t = "bdev";
12457# endif
12458# ifdef S_ISCHR
12459 else if (S_ISCHR(st.st_mode))
12460 t = "cdev";
12461# endif
12462# ifdef S_ISFIFO
12463 else if (S_ISFIFO(st.st_mode))
12464 t = "fifo";
12465# endif
12466# ifdef S_ISSOCK
12467 else if (S_ISSOCK(st.st_mode))
12468 t = "fifo";
12469# endif
12470 else
12471 t = "other";
12472#else
12473# ifdef S_IFMT
12474 switch (st.st_mode & S_IFMT)
12475 {
12476 case S_IFREG: t = "file"; break;
12477 case S_IFDIR: t = "dir"; break;
12478# ifdef S_IFLNK
12479 case S_IFLNK: t = "link"; break;
12480# endif
12481# ifdef S_IFBLK
12482 case S_IFBLK: t = "bdev"; break;
12483# endif
12484# ifdef S_IFCHR
12485 case S_IFCHR: t = "cdev"; break;
12486# endif
12487# ifdef S_IFIFO
12488 case S_IFIFO: t = "fifo"; break;
12489# endif
12490# ifdef S_IFSOCK
12491 case S_IFSOCK: t = "socket"; break;
12492# endif
12493 default: t = "other";
12494 }
12495# else
12496 if (mch_isdir(fname))
12497 t = "dir";
12498 else
12499 t = "file";
12500# endif
12501#endif
12502 type = vim_strsave((char_u *)t);
12503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012505}
12506
12507/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012508 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012509 */
12510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012511f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012512{
12513 linenr_T lnum;
12514 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012515 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516
12517 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012518 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012519 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012520 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012521 retlist = FALSE;
12522 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012523 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012524 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012525 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012526 retlist = TRUE;
12527 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012528
Bram Moolenaar342337a2005-07-21 21:11:17 +000012529 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012530}
12531
12532/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012533 * "getmatches()" function
12534 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012536f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012537{
12538#ifdef FEAT_SEARCH_EXTRA
12539 dict_T *dict;
12540 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012541 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012542
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012543 if (rettv_list_alloc(rettv) == OK)
12544 {
12545 while (cur != NULL)
12546 {
12547 dict = dict_alloc();
12548 if (dict == NULL)
12549 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012550 if (cur->match.regprog == NULL)
12551 {
12552 /* match added with matchaddpos() */
12553 for (i = 0; i < MAXPOSMATCH; ++i)
12554 {
12555 llpos_T *llpos;
12556 char buf[6];
12557 list_T *l;
12558
12559 llpos = &cur->pos.pos[i];
12560 if (llpos->lnum == 0)
12561 break;
12562 l = list_alloc();
12563 if (l == NULL)
12564 break;
12565 list_append_number(l, (varnumber_T)llpos->lnum);
12566 if (llpos->col > 0)
12567 {
12568 list_append_number(l, (varnumber_T)llpos->col);
12569 list_append_number(l, (varnumber_T)llpos->len);
12570 }
12571 sprintf(buf, "pos%d", i + 1);
12572 dict_add_list(dict, buf, l);
12573 }
12574 }
12575 else
12576 {
12577 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12578 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012579 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012580 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12581 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012582# ifdef FEAT_CONCEAL
12583 if (cur->conceal_char)
12584 {
12585 char_u buf[MB_MAXBYTES + 1];
12586
12587 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12588 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12589 }
12590# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012591 list_append_dict(rettv->vval.v_list, dict);
12592 cur = cur->next;
12593 }
12594 }
12595#endif
12596}
12597
12598/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012599 * "getpid()" function
12600 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012602f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012603{
12604 rettv->vval.v_number = mch_get_pid();
12605}
12606
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012607static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012608
12609/*
12610 * "getcurpos()" function
12611 */
12612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012613f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012614{
12615 getpos_both(argvars, rettv, TRUE);
12616}
12617
Bram Moolenaar18081e32008-02-20 19:11:07 +000012618/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012619 * "getpos(string)" function
12620 */
12621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012622f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012623{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012624 getpos_both(argvars, rettv, FALSE);
12625}
12626
12627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012628getpos_both(
12629 typval_T *argvars,
12630 typval_T *rettv,
12631 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012632{
Bram Moolenaara5525202006-03-02 22:52:09 +000012633 pos_T *fp;
12634 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012635 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012636
12637 if (rettv_list_alloc(rettv) == OK)
12638 {
12639 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012640 if (getcurpos)
12641 fp = &curwin->w_cursor;
12642 else
12643 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012644 if (fnum != -1)
12645 list_append_number(l, (varnumber_T)fnum);
12646 else
12647 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012648 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12649 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012650 list_append_number(l, (fp != NULL)
12651 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012652 : (varnumber_T)0);
12653 list_append_number(l,
12654#ifdef FEAT_VIRTUALEDIT
12655 (fp != NULL) ? (varnumber_T)fp->coladd :
12656#endif
12657 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012658 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012659 {
12660 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012661 list_append_number(l, curwin->w_curswant == MAXCOL ?
12662 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012663 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012664 }
12665 else
12666 rettv->vval.v_number = FALSE;
12667}
12668
12669/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012670 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012671 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012673f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012674{
12675#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012676 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012677#endif
12678
Bram Moolenaar2641f772005-03-25 21:58:17 +000012679#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012680 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012681 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012682 wp = NULL;
12683 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12684 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012685 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012686 if (wp == NULL)
12687 return;
12688 }
12689
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012690 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012691 }
12692#endif
12693}
12694
12695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 * "getreg()" function
12697 */
12698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012699f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700{
12701 char_u *strregname;
12702 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012703 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012704 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012705 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012707 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 strregname = get_tv_string_chk(&argvars[0]);
12710 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012711 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012713 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012714 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12715 return_list = get_tv_number_chk(&argvars[2], &error);
12716 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012719 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012720
12721 if (error)
12722 return;
12723
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 regname = (strregname == NULL ? '"' : *strregname);
12725 if (regname == 0)
12726 regname = '"';
12727
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012728 if (return_list)
12729 {
12730 rettv->v_type = VAR_LIST;
12731 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12732 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012733 if (rettv->vval.v_list != NULL)
12734 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012735 }
12736 else
12737 {
12738 rettv->v_type = VAR_STRING;
12739 rettv->vval.v_string = get_reg_contents(regname,
12740 arg2 ? GREG_EXPR_SRC : 0);
12741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742}
12743
12744/*
12745 * "getregtype()" function
12746 */
12747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012748f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749{
12750 char_u *strregname;
12751 int regname;
12752 char_u buf[NUMBUFLEN + 2];
12753 long reglen = 0;
12754
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012755 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 {
12757 strregname = get_tv_string_chk(&argvars[0]);
12758 if (strregname == NULL) /* type error; errmsg already given */
12759 {
12760 rettv->v_type = VAR_STRING;
12761 rettv->vval.v_string = NULL;
12762 return;
12763 }
12764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 else
12766 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012767 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768
12769 regname = (strregname == NULL ? '"' : *strregname);
12770 if (regname == 0)
12771 regname = '"';
12772
12773 buf[0] = NUL;
12774 buf[1] = NUL;
12775 switch (get_reg_type(regname, &reglen))
12776 {
12777 case MLINE: buf[0] = 'V'; break;
12778 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 case MBLOCK:
12780 buf[0] = Ctrl_V;
12781 sprintf((char *)buf + 1, "%ld", reglen + 1);
12782 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784 rettv->v_type = VAR_STRING;
12785 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786}
12787
12788/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012789 * "gettabvar()" function
12790 */
12791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012792f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012793{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012794 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012795 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012796 dictitem_T *v;
12797 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012798 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012799
12800 rettv->v_type = VAR_STRING;
12801 rettv->vval.v_string = NULL;
12802
12803 varname = get_tv_string_chk(&argvars[1]);
12804 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12805 if (tp != NULL && varname != NULL)
12806 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012807 /* Set tp to be our tabpage, temporarily. Also set the window to the
12808 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012809 if (switch_win(&oldcurwin, &oldtabpage,
12810 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012811 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012812 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012813 /* look up the variable */
12814 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12815 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12816 if (v != NULL)
12817 {
12818 copy_tv(&v->di_tv, rettv);
12819 done = TRUE;
12820 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012821 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012822
12823 /* restore previous notion of curwin */
12824 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012825 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012826
12827 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012828 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012829}
12830
12831/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012832 * "gettabwinvar()" function
12833 */
12834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012835f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012836{
12837 getwinvar(argvars, rettv, 1);
12838}
12839
12840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 * "getwinposx()" function
12842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012844f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847#ifdef FEAT_GUI
12848 if (gui.in_use)
12849 {
12850 int x, y;
12851
12852 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 }
12855#endif
12856}
12857
12858/*
12859 * "getwinposy()" function
12860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012862f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865#ifdef FEAT_GUI
12866 if (gui.in_use)
12867 {
12868 int x, y;
12869
12870 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 }
12873#endif
12874}
12875
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012876/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012877 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012878 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012879 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012880find_win_by_nr(
12881 typval_T *vp,
12882 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012883{
12884#ifdef FEAT_WINDOWS
12885 win_T *wp;
12886#endif
12887 int nr;
12888
12889 nr = get_tv_number_chk(vp, NULL);
12890
12891#ifdef FEAT_WINDOWS
12892 if (nr < 0)
12893 return NULL;
12894 if (nr == 0)
12895 return curwin;
12896
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012897 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12898 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012899 if (--nr <= 0)
12900 break;
12901 return wp;
12902#else
12903 if (nr == 0 || nr == 1)
12904 return curwin;
12905 return NULL;
12906#endif
12907}
12908
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012910 * Find window specified by "wvp" in tabpage "tvp".
12911 */
12912 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012913find_tabwin(
12914 typval_T *wvp, /* VAR_UNKNOWN for current window */
12915 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012916{
12917 win_T *wp = NULL;
12918 tabpage_T *tp = NULL;
12919 long n;
12920
12921 if (wvp->v_type != VAR_UNKNOWN)
12922 {
12923 if (tvp->v_type != VAR_UNKNOWN)
12924 {
12925 n = get_tv_number(tvp);
12926 if (n >= 0)
12927 tp = find_tabpage(n);
12928 }
12929 else
12930 tp = curtab;
12931
12932 if (tp != NULL)
12933 wp = find_win_by_nr(wvp, tp);
12934 }
12935 else
12936 wp = curwin;
12937
12938 return wp;
12939}
12940
12941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942 * "getwinvar()" function
12943 */
12944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012945f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012947 getwinvar(argvars, rettv, 0);
12948}
12949
12950/*
12951 * getwinvar() and gettabwinvar()
12952 */
12953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012954getwinvar(
12955 typval_T *argvars,
12956 typval_T *rettv,
12957 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012958{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012959 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012961 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012962 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012963 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012964#ifdef FEAT_WINDOWS
12965 win_T *oldcurwin;
12966 tabpage_T *oldtabpage;
12967 int need_switch_win;
12968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012970#ifdef FEAT_WINDOWS
12971 if (off == 1)
12972 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12973 else
12974 tp = curtab;
12975#endif
12976 win = find_win_by_nr(&argvars[off], tp);
12977 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012978 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012980 rettv->v_type = VAR_STRING;
12981 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982
12983 if (win != NULL && varname != NULL)
12984 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012985#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012986 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012987 * otherwise the window is not valid. Only do this when needed,
12988 * autocommands get blocked. */
12989 need_switch_win = !(tp == curtab && win == curwin);
12990 if (!need_switch_win
12991 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12992#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012993 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012994 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012995 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012996 if (get_option_tv(&varname, rettv, 1) == OK)
12997 done = TRUE;
12998 }
12999 else
13000 {
13001 /* Look up the variable. */
13002 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13003 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13004 varname, FALSE);
13005 if (v != NULL)
13006 {
13007 copy_tv(&v->di_tv, rettv);
13008 done = TRUE;
13009 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013012
Bram Moolenaarba117c22015-09-29 16:53:22 +020013013#ifdef FEAT_WINDOWS
13014 if (need_switch_win)
13015 /* restore previous notion of curwin */
13016 restore_win(oldcurwin, oldtabpage, TRUE);
13017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018 }
13019
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013020 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13021 /* use the default return value */
13022 copy_tv(&argvars[off + 2], rettv);
13023
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024 --emsg_off;
13025}
13026
13027/*
13028 * "glob()" function
13029 */
13030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013031f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013033 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013035 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013037 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013038 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013039 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013040 if (argvars[1].v_type != VAR_UNKNOWN)
13041 {
13042 if (get_tv_number_chk(&argvars[1], &error))
13043 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013044 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013045 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013046 if (get_tv_number_chk(&argvars[2], &error))
13047 {
13048 rettv->v_type = VAR_LIST;
13049 rettv->vval.v_list = NULL;
13050 }
13051 if (argvars[3].v_type != VAR_UNKNOWN
13052 && get_tv_number_chk(&argvars[3], &error))
13053 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013054 }
13055 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013056 if (!error)
13057 {
13058 ExpandInit(&xpc);
13059 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013060 if (p_wic)
13061 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013062 if (rettv->v_type == VAR_STRING)
13063 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013064 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013065 else if (rettv_list_alloc(rettv) != FAIL)
13066 {
13067 int i;
13068
13069 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13070 NULL, options, WILD_ALL_KEEP);
13071 for (i = 0; i < xpc.xp_numfiles; i++)
13072 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13073
13074 ExpandCleanup(&xpc);
13075 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013076 }
13077 else
13078 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079}
13080
13081/*
13082 * "globpath()" function
13083 */
13084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013085f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013087 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013089 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013090 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013091 garray_T ga;
13092 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013094 /* When the optional second argument is non-zero, don't remove matches
13095 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013096 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013097 if (argvars[2].v_type != VAR_UNKNOWN)
13098 {
13099 if (get_tv_number_chk(&argvars[2], &error))
13100 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013101 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013102 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013103 if (get_tv_number_chk(&argvars[3], &error))
13104 {
13105 rettv->v_type = VAR_LIST;
13106 rettv->vval.v_list = NULL;
13107 }
13108 if (argvars[4].v_type != VAR_UNKNOWN
13109 && get_tv_number_chk(&argvars[4], &error))
13110 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013111 }
13112 }
13113 if (file != NULL && !error)
13114 {
13115 ga_init2(&ga, (int)sizeof(char_u *), 10);
13116 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13117 if (rettv->v_type == VAR_STRING)
13118 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13119 else if (rettv_list_alloc(rettv) != FAIL)
13120 for (i = 0; i < ga.ga_len; ++i)
13121 list_append_string(rettv->vval.v_list,
13122 ((char_u **)(ga.ga_data))[i], -1);
13123 ga_clear_strings(&ga);
13124 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013125 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013126 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127}
13128
13129/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013130 * "glob2regpat()" function
13131 */
13132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013133f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013134{
13135 char_u *pat = get_tv_string_chk(&argvars[0]);
13136
13137 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013138 rettv->vval.v_string = (pat == NULL)
13139 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013140}
13141
13142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 * "has()" function
13144 */
13145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013146f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147{
13148 int i;
13149 char_u *name;
13150 int n = FALSE;
13151 static char *(has_list[]) =
13152 {
13153#ifdef AMIGA
13154 "amiga",
13155# ifdef FEAT_ARP
13156 "arp",
13157# endif
13158#endif
13159#ifdef __BEOS__
13160 "beos",
13161#endif
13162#ifdef MSDOS
13163# ifdef DJGPP
13164 "dos32",
13165# else
13166 "dos16",
13167# endif
13168#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013169#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 "mac",
13171#endif
13172#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013173 "macunix", /* built with 'darwin' enabled */
13174#endif
13175#if defined(__APPLE__) && __APPLE__ == 1
13176 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178#ifdef __QNX__
13179 "qnx",
13180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181#ifdef UNIX
13182 "unix",
13183#endif
13184#ifdef VMS
13185 "vms",
13186#endif
13187#ifdef WIN16
13188 "win16",
13189#endif
13190#ifdef WIN32
13191 "win32",
13192#endif
13193#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13194 "win32unix",
13195#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013196#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197 "win64",
13198#endif
13199#ifdef EBCDIC
13200 "ebcdic",
13201#endif
13202#ifndef CASE_INSENSITIVE_FILENAME
13203 "fname_case",
13204#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013205#ifdef HAVE_ACL
13206 "acl",
13207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208#ifdef FEAT_ARABIC
13209 "arabic",
13210#endif
13211#ifdef FEAT_AUTOCMD
13212 "autocmd",
13213#endif
13214#ifdef FEAT_BEVAL
13215 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013216# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13217 "balloon_multiline",
13218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219#endif
13220#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13221 "builtin_terms",
13222# ifdef ALL_BUILTIN_TCAPS
13223 "all_builtin_terms",
13224# endif
13225#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013226#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13227 || defined(FEAT_GUI_W32) \
13228 || defined(FEAT_GUI_MOTIF))
13229 "browsefilter",
13230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231#ifdef FEAT_BYTEOFF
13232 "byte_offset",
13233#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013234#ifdef FEAT_CHANNEL
13235 "channel",
13236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237#ifdef FEAT_CINDENT
13238 "cindent",
13239#endif
13240#ifdef FEAT_CLIENTSERVER
13241 "clientserver",
13242#endif
13243#ifdef FEAT_CLIPBOARD
13244 "clipboard",
13245#endif
13246#ifdef FEAT_CMDL_COMPL
13247 "cmdline_compl",
13248#endif
13249#ifdef FEAT_CMDHIST
13250 "cmdline_hist",
13251#endif
13252#ifdef FEAT_COMMENTS
13253 "comments",
13254#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013255#ifdef FEAT_CONCEAL
13256 "conceal",
13257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258#ifdef FEAT_CRYPT
13259 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013260 "crypt-blowfish",
13261 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262#endif
13263#ifdef FEAT_CSCOPE
13264 "cscope",
13265#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013266#ifdef FEAT_CURSORBIND
13267 "cursorbind",
13268#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013269#ifdef CURSOR_SHAPE
13270 "cursorshape",
13271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272#ifdef DEBUG
13273 "debug",
13274#endif
13275#ifdef FEAT_CON_DIALOG
13276 "dialog_con",
13277#endif
13278#ifdef FEAT_GUI_DIALOG
13279 "dialog_gui",
13280#endif
13281#ifdef FEAT_DIFF
13282 "diff",
13283#endif
13284#ifdef FEAT_DIGRAPHS
13285 "digraphs",
13286#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013287#ifdef FEAT_DIRECTX
13288 "directx",
13289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290#ifdef FEAT_DND
13291 "dnd",
13292#endif
13293#ifdef FEAT_EMACS_TAGS
13294 "emacs_tags",
13295#endif
13296 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013297 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298#ifdef FEAT_SEARCH_EXTRA
13299 "extra_search",
13300#endif
13301#ifdef FEAT_FKMAP
13302 "farsi",
13303#endif
13304#ifdef FEAT_SEARCHPATH
13305 "file_in_path",
13306#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013307#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013308 "filterpipe",
13309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310#ifdef FEAT_FIND_ID
13311 "find_in_path",
13312#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013313#ifdef FEAT_FLOAT
13314 "float",
13315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316#ifdef FEAT_FOLDING
13317 "folding",
13318#endif
13319#ifdef FEAT_FOOTER
13320 "footer",
13321#endif
13322#if !defined(USE_SYSTEM) && defined(UNIX)
13323 "fork",
13324#endif
13325#ifdef FEAT_GETTEXT
13326 "gettext",
13327#endif
13328#ifdef FEAT_GUI
13329 "gui",
13330#endif
13331#ifdef FEAT_GUI_ATHENA
13332# ifdef FEAT_GUI_NEXTAW
13333 "gui_neXtaw",
13334# else
13335 "gui_athena",
13336# endif
13337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338#ifdef FEAT_GUI_GTK
13339 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013342#ifdef FEAT_GUI_GNOME
13343 "gui_gnome",
13344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345#ifdef FEAT_GUI_MAC
13346 "gui_mac",
13347#endif
13348#ifdef FEAT_GUI_MOTIF
13349 "gui_motif",
13350#endif
13351#ifdef FEAT_GUI_PHOTON
13352 "gui_photon",
13353#endif
13354#ifdef FEAT_GUI_W16
13355 "gui_win16",
13356#endif
13357#ifdef FEAT_GUI_W32
13358 "gui_win32",
13359#endif
13360#ifdef FEAT_HANGULIN
13361 "hangul_input",
13362#endif
13363#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13364 "iconv",
13365#endif
13366#ifdef FEAT_INS_EXPAND
13367 "insert_expand",
13368#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013369#ifdef FEAT_JOB
13370 "job",
13371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372#ifdef FEAT_JUMPLIST
13373 "jumplist",
13374#endif
13375#ifdef FEAT_KEYMAP
13376 "keymap",
13377#endif
13378#ifdef FEAT_LANGMAP
13379 "langmap",
13380#endif
13381#ifdef FEAT_LIBCALL
13382 "libcall",
13383#endif
13384#ifdef FEAT_LINEBREAK
13385 "linebreak",
13386#endif
13387#ifdef FEAT_LISP
13388 "lispindent",
13389#endif
13390#ifdef FEAT_LISTCMDS
13391 "listcmds",
13392#endif
13393#ifdef FEAT_LOCALMAP
13394 "localmap",
13395#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013396#ifdef FEAT_LUA
13397# ifndef DYNAMIC_LUA
13398 "lua",
13399# endif
13400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401#ifdef FEAT_MENU
13402 "menu",
13403#endif
13404#ifdef FEAT_SESSION
13405 "mksession",
13406#endif
13407#ifdef FEAT_MODIFY_FNAME
13408 "modify_fname",
13409#endif
13410#ifdef FEAT_MOUSE
13411 "mouse",
13412#endif
13413#ifdef FEAT_MOUSESHAPE
13414 "mouseshape",
13415#endif
13416#if defined(UNIX) || defined(VMS)
13417# ifdef FEAT_MOUSE_DEC
13418 "mouse_dec",
13419# endif
13420# ifdef FEAT_MOUSE_GPM
13421 "mouse_gpm",
13422# endif
13423# ifdef FEAT_MOUSE_JSB
13424 "mouse_jsbterm",
13425# endif
13426# ifdef FEAT_MOUSE_NET
13427 "mouse_netterm",
13428# endif
13429# ifdef FEAT_MOUSE_PTERM
13430 "mouse_pterm",
13431# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013432# ifdef FEAT_MOUSE_SGR
13433 "mouse_sgr",
13434# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013435# ifdef FEAT_SYSMOUSE
13436 "mouse_sysmouse",
13437# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013438# ifdef FEAT_MOUSE_URXVT
13439 "mouse_urxvt",
13440# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441# ifdef FEAT_MOUSE_XTERM
13442 "mouse_xterm",
13443# endif
13444#endif
13445#ifdef FEAT_MBYTE
13446 "multi_byte",
13447#endif
13448#ifdef FEAT_MBYTE_IME
13449 "multi_byte_ime",
13450#endif
13451#ifdef FEAT_MULTI_LANG
13452 "multi_lang",
13453#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013454#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013455#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013456 "mzscheme",
13457#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459#ifdef FEAT_OLE
13460 "ole",
13461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462#ifdef FEAT_PATH_EXTRA
13463 "path_extra",
13464#endif
13465#ifdef FEAT_PERL
13466#ifndef DYNAMIC_PERL
13467 "perl",
13468#endif
13469#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013470#ifdef FEAT_PERSISTENT_UNDO
13471 "persistent_undo",
13472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473#ifdef FEAT_PYTHON
13474#ifndef DYNAMIC_PYTHON
13475 "python",
13476#endif
13477#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013478#ifdef FEAT_PYTHON3
13479#ifndef DYNAMIC_PYTHON3
13480 "python3",
13481#endif
13482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483#ifdef FEAT_POSTSCRIPT
13484 "postscript",
13485#endif
13486#ifdef FEAT_PRINTER
13487 "printer",
13488#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013489#ifdef FEAT_PROFILE
13490 "profile",
13491#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013492#ifdef FEAT_RELTIME
13493 "reltime",
13494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495#ifdef FEAT_QUICKFIX
13496 "quickfix",
13497#endif
13498#ifdef FEAT_RIGHTLEFT
13499 "rightleft",
13500#endif
13501#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13502 "ruby",
13503#endif
13504#ifdef FEAT_SCROLLBIND
13505 "scrollbind",
13506#endif
13507#ifdef FEAT_CMDL_INFO
13508 "showcmd",
13509 "cmdline_info",
13510#endif
13511#ifdef FEAT_SIGNS
13512 "signs",
13513#endif
13514#ifdef FEAT_SMARTINDENT
13515 "smartindent",
13516#endif
13517#ifdef FEAT_SNIFF
13518 "sniff",
13519#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013520#ifdef STARTUPTIME
13521 "startuptime",
13522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523#ifdef FEAT_STL_OPT
13524 "statusline",
13525#endif
13526#ifdef FEAT_SUN_WORKSHOP
13527 "sun_workshop",
13528#endif
13529#ifdef FEAT_NETBEANS_INTG
13530 "netbeans_intg",
13531#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013532#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013533 "spell",
13534#endif
13535#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536 "syntax",
13537#endif
13538#if defined(USE_SYSTEM) || !defined(UNIX)
13539 "system",
13540#endif
13541#ifdef FEAT_TAG_BINS
13542 "tag_binary",
13543#endif
13544#ifdef FEAT_TAG_OLDSTATIC
13545 "tag_old_static",
13546#endif
13547#ifdef FEAT_TAG_ANYWHITE
13548 "tag_any_white",
13549#endif
13550#ifdef FEAT_TCL
13551# ifndef DYNAMIC_TCL
13552 "tcl",
13553# endif
13554#endif
13555#ifdef TERMINFO
13556 "terminfo",
13557#endif
13558#ifdef FEAT_TERMRESPONSE
13559 "termresponse",
13560#endif
13561#ifdef FEAT_TEXTOBJ
13562 "textobjects",
13563#endif
13564#ifdef HAVE_TGETENT
13565 "tgetent",
13566#endif
13567#ifdef FEAT_TITLE
13568 "title",
13569#endif
13570#ifdef FEAT_TOOLBAR
13571 "toolbar",
13572#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013573#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13574 "unnamedplus",
13575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576#ifdef FEAT_USR_CMDS
13577 "user-commands", /* was accidentally included in 5.4 */
13578 "user_commands",
13579#endif
13580#ifdef FEAT_VIMINFO
13581 "viminfo",
13582#endif
13583#ifdef FEAT_VERTSPLIT
13584 "vertsplit",
13585#endif
13586#ifdef FEAT_VIRTUALEDIT
13587 "virtualedit",
13588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590#ifdef FEAT_VISUALEXTRA
13591 "visualextra",
13592#endif
13593#ifdef FEAT_VREPLACE
13594 "vreplace",
13595#endif
13596#ifdef FEAT_WILDIGN
13597 "wildignore",
13598#endif
13599#ifdef FEAT_WILDMENU
13600 "wildmenu",
13601#endif
13602#ifdef FEAT_WINDOWS
13603 "windows",
13604#endif
13605#ifdef FEAT_WAK
13606 "winaltkeys",
13607#endif
13608#ifdef FEAT_WRITEBACKUP
13609 "writebackup",
13610#endif
13611#ifdef FEAT_XIM
13612 "xim",
13613#endif
13614#ifdef FEAT_XFONTSET
13615 "xfontset",
13616#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013617#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013618 "xpm",
13619 "xpm_w32", /* for backward compatibility */
13620#else
13621# if defined(HAVE_XPM)
13622 "xpm",
13623# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625#ifdef USE_XSMP
13626 "xsmp",
13627#endif
13628#ifdef USE_XSMP_INTERACT
13629 "xsmp_interact",
13630#endif
13631#ifdef FEAT_XCLIPBOARD
13632 "xterm_clipboard",
13633#endif
13634#ifdef FEAT_XTERM_SAVE
13635 "xterm_save",
13636#endif
13637#if defined(UNIX) && defined(FEAT_X11)
13638 "X11",
13639#endif
13640 NULL
13641 };
13642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 for (i = 0; has_list[i] != NULL; ++i)
13645 if (STRICMP(name, has_list[i]) == 0)
13646 {
13647 n = TRUE;
13648 break;
13649 }
13650
13651 if (n == FALSE)
13652 {
13653 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013654 {
13655 if (name[5] == '-'
13656 && STRLEN(name) > 11
13657 && vim_isdigit(name[6])
13658 && vim_isdigit(name[8])
13659 && vim_isdigit(name[10]))
13660 {
13661 int major = atoi((char *)name + 6);
13662 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013663
13664 /* Expect "patch-9.9.01234". */
13665 n = (major < VIM_VERSION_MAJOR
13666 || (major == VIM_VERSION_MAJOR
13667 && (minor < VIM_VERSION_MINOR
13668 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013669 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013670 }
13671 else
13672 n = has_patch(atoi((char *)name + 5));
13673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 else if (STRICMP(name, "vim_starting") == 0)
13675 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013676#ifdef FEAT_MBYTE
13677 else if (STRICMP(name, "multi_byte_encoding") == 0)
13678 n = has_mbyte;
13679#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013680#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13681 else if (STRICMP(name, "balloon_multiline") == 0)
13682 n = multiline_balloon_available();
13683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684#ifdef DYNAMIC_TCL
13685 else if (STRICMP(name, "tcl") == 0)
13686 n = tcl_enabled(FALSE);
13687#endif
13688#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13689 else if (STRICMP(name, "iconv") == 0)
13690 n = iconv_enabled(FALSE);
13691#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013692#ifdef DYNAMIC_LUA
13693 else if (STRICMP(name, "lua") == 0)
13694 n = lua_enabled(FALSE);
13695#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013696#ifdef DYNAMIC_MZSCHEME
13697 else if (STRICMP(name, "mzscheme") == 0)
13698 n = mzscheme_enabled(FALSE);
13699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700#ifdef DYNAMIC_RUBY
13701 else if (STRICMP(name, "ruby") == 0)
13702 n = ruby_enabled(FALSE);
13703#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013704#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705#ifdef DYNAMIC_PYTHON
13706 else if (STRICMP(name, "python") == 0)
13707 n = python_enabled(FALSE);
13708#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013709#endif
13710#ifdef FEAT_PYTHON3
13711#ifdef DYNAMIC_PYTHON3
13712 else if (STRICMP(name, "python3") == 0)
13713 n = python3_enabled(FALSE);
13714#endif
13715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716#ifdef DYNAMIC_PERL
13717 else if (STRICMP(name, "perl") == 0)
13718 n = perl_enabled(FALSE);
13719#endif
13720#ifdef FEAT_GUI
13721 else if (STRICMP(name, "gui_running") == 0)
13722 n = (gui.in_use || gui.starting);
13723# ifdef FEAT_GUI_W32
13724 else if (STRICMP(name, "gui_win32s") == 0)
13725 n = gui_is_win32s();
13726# endif
13727# ifdef FEAT_BROWSE
13728 else if (STRICMP(name, "browse") == 0)
13729 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13730# endif
13731#endif
13732#ifdef FEAT_SYN_HL
13733 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013734 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735#endif
13736#if defined(WIN3264)
13737 else if (STRICMP(name, "win95") == 0)
13738 n = mch_windows95();
13739#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013740#ifdef FEAT_NETBEANS_INTG
13741 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013742 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 }
13745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747}
13748
13749/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013750 * "has_key()" function
13751 */
13752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013753f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013754{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013755 if (argvars[0].v_type != VAR_DICT)
13756 {
13757 EMSG(_(e_dictreq));
13758 return;
13759 }
13760 if (argvars[0].vval.v_dict == NULL)
13761 return;
13762
13763 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013764 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013765}
13766
13767/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013768 * "haslocaldir()" function
13769 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013771f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013772{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013773 win_T *wp = NULL;
13774
13775 wp = find_tabwin(&argvars[0], &argvars[1]);
13776 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013777}
13778
13779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780 * "hasmapto()" function
13781 */
13782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013783f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784{
13785 char_u *name;
13786 char_u *mode;
13787 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013788 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013790 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013791 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 mode = (char_u *)"nvo";
13793 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013796 if (argvars[2].v_type != VAR_UNKNOWN)
13797 abbr = get_tv_number(&argvars[2]);
13798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799
Bram Moolenaar2c932302006-03-18 21:42:09 +000013800 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013801 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013803 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804}
13805
13806/*
13807 * "histadd()" function
13808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013810f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811{
13812#ifdef FEAT_CMDHIST
13813 int histype;
13814 char_u *str;
13815 char_u buf[NUMBUFLEN];
13816#endif
13817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 if (check_restricted() || check_secure())
13820 return;
13821#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13823 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 if (histype >= 0)
13825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 if (*str != NUL)
13828 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013829 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013831 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 return;
13833 }
13834 }
13835#endif
13836}
13837
13838/*
13839 * "histdel()" function
13840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013842f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843{
13844#ifdef FEAT_CMDHIST
13845 int n;
13846 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013847 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013849 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13850 if (str == NULL)
13851 n = 0;
13852 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013854 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013855 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013857 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013858 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859 else
13860 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013862 get_tv_string_buf(&argvars[1], buf));
13863 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864#endif
13865}
13866
13867/*
13868 * "histget()" function
13869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013871f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872{
13873#ifdef FEAT_CMDHIST
13874 int type;
13875 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013876 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013878 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13879 if (str == NULL)
13880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013882 {
13883 type = get_histtype(str);
13884 if (argvars[1].v_type == VAR_UNKNOWN)
13885 idx = get_history_idx(type);
13886 else
13887 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13888 /* -1 on type error */
13889 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013892 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013894 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895}
13896
13897/*
13898 * "histnr()" function
13899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013901f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902{
13903 int i;
13904
13905#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013906 char_u *history = get_tv_string_chk(&argvars[0]);
13907
13908 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 if (i >= HIST_CMD && i < HIST_COUNT)
13910 i = get_history_idx(i);
13911 else
13912#endif
13913 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915}
13916
13917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 * "highlightID(name)" function
13919 */
13920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013921f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013923 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924}
13925
13926/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013927 * "highlight_exists()" function
13928 */
13929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013930f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013931{
13932 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13933}
13934
13935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 * "hostname()" function
13937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013939f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940{
13941 char_u hostname[256];
13942
13943 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944 rettv->v_type = VAR_STRING;
13945 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946}
13947
13948/*
13949 * iconv() function
13950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013952f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953{
13954#ifdef FEAT_MBYTE
13955 char_u buf1[NUMBUFLEN];
13956 char_u buf2[NUMBUFLEN];
13957 char_u *from, *to, *str;
13958 vimconv_T vimconv;
13959#endif
13960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013961 rettv->v_type = VAR_STRING;
13962 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963
13964#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013965 str = get_tv_string(&argvars[0]);
13966 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13967 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 vimconv.vc_type = CONV_NONE;
13969 convert_setup(&vimconv, from, to);
13970
13971 /* If the encodings are equal, no conversion needed. */
13972 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013973 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976
13977 convert_setup(&vimconv, NULL, NULL);
13978 vim_free(from);
13979 vim_free(to);
13980#endif
13981}
13982
13983/*
13984 * "indent()" function
13985 */
13986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013987f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988{
13989 linenr_T lnum;
13990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013993 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996}
13997
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013998/*
13999 * "index()" function
14000 */
14001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014002f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014003{
Bram Moolenaar33570922005-01-25 22:26:29 +000014004 list_T *l;
14005 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014006 long idx = 0;
14007 int ic = FALSE;
14008
14009 rettv->vval.v_number = -1;
14010 if (argvars[0].v_type != VAR_LIST)
14011 {
14012 EMSG(_(e_listreq));
14013 return;
14014 }
14015 l = argvars[0].vval.v_list;
14016 if (l != NULL)
14017 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014018 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014019 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021 int error = FALSE;
14022
Bram Moolenaar758711c2005-02-02 23:11:38 +000014023 /* Start at specified item. Use the cached index that list_find()
14024 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014025 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014026 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014027 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014028 ic = get_tv_number_chk(&argvars[3], &error);
14029 if (error)
14030 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014031 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014032
Bram Moolenaar758711c2005-02-02 23:11:38 +000014033 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014034 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014035 {
14036 rettv->vval.v_number = idx;
14037 break;
14038 }
14039 }
14040}
14041
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042static int inputsecret_flag = 0;
14043
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014044static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014045
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014047 * This function is used by f_input() and f_inputdialog() functions. The third
14048 * argument to f_input() specifies the type of completion to use at the
14049 * prompt. The third argument to f_inputdialog() specifies the value to return
14050 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051 */
14052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014053get_user_input(
14054 typval_T *argvars,
14055 typval_T *rettv,
14056 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014058 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 char_u *p = NULL;
14060 int c;
14061 char_u buf[NUMBUFLEN];
14062 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014063 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014064 int xp_type = EXPAND_NOTHING;
14065 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014067 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069
14070#ifdef NO_CONSOLE_INPUT
14071 /* While starting up, there is no place to enter text. */
14072 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074#endif
14075
14076 cmd_silent = FALSE; /* Want to see the prompt. */
14077 if (prompt != NULL)
14078 {
14079 /* Only the part of the message after the last NL is considered as
14080 * prompt for the command line */
14081 p = vim_strrchr(prompt, '\n');
14082 if (p == NULL)
14083 p = prompt;
14084 else
14085 {
14086 ++p;
14087 c = *p;
14088 *p = NUL;
14089 msg_start();
14090 msg_clr_eos();
14091 msg_puts_attr(prompt, echo_attr);
14092 msg_didout = FALSE;
14093 msg_starthere();
14094 *p = c;
14095 }
14096 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014098 if (argvars[1].v_type != VAR_UNKNOWN)
14099 {
14100 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14101 if (defstr != NULL)
14102 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014104 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014105 {
14106 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014107 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014108 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014109
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014110 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014111 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014112
Bram Moolenaar4463f292005-09-25 22:20:24 +000014113 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14114 if (xp_name == NULL)
14115 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014116
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014117 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014118
Bram Moolenaar4463f292005-09-25 22:20:24 +000014119 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14120 &xp_arg) == FAIL)
14121 return;
14122 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014123 }
14124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014125 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014126 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014127 int save_ex_normal_busy = ex_normal_busy;
14128 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014129 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014130 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14131 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014132 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014133 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014134 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014135 && argvars[1].v_type != VAR_UNKNOWN
14136 && argvars[2].v_type != VAR_UNKNOWN)
14137 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14138 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014139
14140 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014142 /* since the user typed this, no need to wait for return */
14143 need_wait_return = FALSE;
14144 msg_didout = FALSE;
14145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 cmd_silent = cmd_silent_save;
14147}
14148
14149/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014150 * "input()" function
14151 * Also handles inputsecret() when inputsecret is set.
14152 */
14153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014154f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014155{
14156 get_user_input(argvars, rettv, FALSE);
14157}
14158
14159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 * "inputdialog()" function
14161 */
14162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014163f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164{
14165#if defined(FEAT_GUI_TEXTDIALOG)
14166 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14167 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14168 {
14169 char_u *message;
14170 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 message = get_tv_string_chk(&argvars[0]);
14174 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014175 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014176 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 else
14178 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 if (message != NULL && defstr != NULL
14180 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014181 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183 else
14184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014185 if (message != NULL && defstr != NULL
14186 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014187 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014188 rettv->vval.v_string = vim_strsave(
14189 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014191 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014193 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 }
14195 else
14196#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014197 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198}
14199
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014200/*
14201 * "inputlist()" function
14202 */
14203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014204f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014205{
14206 listitem_T *li;
14207 int selected;
14208 int mouse_used;
14209
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014210#ifdef NO_CONSOLE_INPUT
14211 /* While starting up, there is no place to enter text. */
14212 if (no_console_input())
14213 return;
14214#endif
14215 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14216 {
14217 EMSG2(_(e_listarg), "inputlist()");
14218 return;
14219 }
14220
14221 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014222 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014223 lines_left = Rows; /* avoid more prompt */
14224 msg_scroll = TRUE;
14225 msg_clr_eos();
14226
14227 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14228 {
14229 msg_puts(get_tv_string(&li->li_tv));
14230 msg_putchar('\n');
14231 }
14232
14233 /* Ask for choice. */
14234 selected = prompt_for_number(&mouse_used);
14235 if (mouse_used)
14236 selected -= lines_left;
14237
14238 rettv->vval.v_number = selected;
14239}
14240
14241
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14243
14244/*
14245 * "inputrestore()" function
14246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014248f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249{
14250 if (ga_userinput.ga_len > 0)
14251 {
14252 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14254 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014255 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 }
14257 else if (p_verbose > 1)
14258 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014259 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 }
14262}
14263
14264/*
14265 * "inputsave()" function
14266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014268f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014270 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 if (ga_grow(&ga_userinput, 1) == OK)
14272 {
14273 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14274 + ga_userinput.ga_len);
14275 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014276 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 }
14278 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280}
14281
14282/*
14283 * "inputsecret()" function
14284 */
14285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014286f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287{
14288 ++cmdline_star;
14289 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014290 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 --cmdline_star;
14292 --inputsecret_flag;
14293}
14294
14295/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014296 * "insert()" function
14297 */
14298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014299f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014300{
14301 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014302 listitem_T *item;
14303 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014305
14306 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014307 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014308 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014309 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014310 {
14311 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 before = get_tv_number_chk(&argvars[2], &error);
14313 if (error)
14314 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014315
Bram Moolenaar758711c2005-02-02 23:11:38 +000014316 if (before == l->lv_len)
14317 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014318 else
14319 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014320 item = list_find(l, before);
14321 if (item == NULL)
14322 {
14323 EMSGN(_(e_listidx), before);
14324 l = NULL;
14325 }
14326 }
14327 if (l != NULL)
14328 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014329 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014330 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014331 }
14332 }
14333}
14334
14335/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014336 * "invert(expr)" function
14337 */
14338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014339f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014340{
14341 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14342}
14343
14344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 * "isdirectory()" function
14346 */
14347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014348f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014350 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351}
14352
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014353/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014354 * "islocked()" function
14355 */
14356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014357f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014358{
14359 lval_T lv;
14360 char_u *end;
14361 dictitem_T *di;
14362
14363 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014364 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14365 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014366 if (end != NULL && lv.ll_name != NULL)
14367 {
14368 if (*end != NUL)
14369 EMSG(_(e_trailing));
14370 else
14371 {
14372 if (lv.ll_tv == NULL)
14373 {
14374 if (check_changedtick(lv.ll_name))
14375 rettv->vval.v_number = 1; /* always locked */
14376 else
14377 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014378 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014379 if (di != NULL)
14380 {
14381 /* Consider a variable locked when:
14382 * 1. the variable itself is locked
14383 * 2. the value of the variable is locked.
14384 * 3. the List or Dict value is locked.
14385 */
14386 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14387 || tv_islocked(&di->di_tv));
14388 }
14389 }
14390 }
14391 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014392 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014393 else if (lv.ll_newkey != NULL)
14394 EMSG2(_(e_dictkey), lv.ll_newkey);
14395 else if (lv.ll_list != NULL)
14396 /* List item. */
14397 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14398 else
14399 /* Dictionary item. */
14400 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14401 }
14402 }
14403
14404 clear_lval(&lv);
14405}
14406
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014407static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014408
14409/*
14410 * Turn a dict into a list:
14411 * "what" == 0: list of keys
14412 * "what" == 1: list of values
14413 * "what" == 2: list of items
14414 */
14415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014416dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014417{
Bram Moolenaar33570922005-01-25 22:26:29 +000014418 list_T *l2;
14419 dictitem_T *di;
14420 hashitem_T *hi;
14421 listitem_T *li;
14422 listitem_T *li2;
14423 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014424 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014425
Bram Moolenaar8c711452005-01-14 21:53:12 +000014426 if (argvars[0].v_type != VAR_DICT)
14427 {
14428 EMSG(_(e_dictreq));
14429 return;
14430 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014431 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014432 return;
14433
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014434 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014435 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014436
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014437 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014438 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014439 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014440 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014441 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014442 --todo;
14443 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014444
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014445 li = listitem_alloc();
14446 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014447 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014448 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014449
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014450 if (what == 0)
14451 {
14452 /* keys() */
14453 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014454 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014455 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14456 }
14457 else if (what == 1)
14458 {
14459 /* values() */
14460 copy_tv(&di->di_tv, &li->li_tv);
14461 }
14462 else
14463 {
14464 /* items() */
14465 l2 = list_alloc();
14466 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014467 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014468 li->li_tv.vval.v_list = l2;
14469 if (l2 == NULL)
14470 break;
14471 ++l2->lv_refcount;
14472
14473 li2 = listitem_alloc();
14474 if (li2 == NULL)
14475 break;
14476 list_append(l2, li2);
14477 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014478 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014479 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14480
14481 li2 = listitem_alloc();
14482 if (li2 == NULL)
14483 break;
14484 list_append(l2, li2);
14485 copy_tv(&di->di_tv, &li2->li_tv);
14486 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014487 }
14488 }
14489}
14490
14491/*
14492 * "items(dict)" function
14493 */
14494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014495f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014496{
14497 dict_list(argvars, rettv, 2);
14498}
14499
Bram Moolenaar835dc632016-02-07 14:27:38 +010014500#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014501
14502# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014503/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014504 * "job_getchannel()" function
14505 */
14506 static void
14507f_job_getchannel(typval_T *argvars, typval_T *rettv)
14508{
14509 if (argvars[0].v_type != VAR_JOB)
14510 EMSG(_(e_invarg));
14511 else
14512 {
14513 job_T *job = argvars[0].vval.v_job;
14514
Bram Moolenaar77073442016-02-13 23:23:53 +010014515 rettv->v_type = VAR_CHANNEL;
14516 rettv->vval.v_channel = job->jv_channel;
14517 if (job->jv_channel != NULL)
14518 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014519 }
14520}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014521# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014522
14523/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014524 * "job_start()" function
14525 */
14526 static void
14527f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14528{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014529 job_T *job;
14530 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014531#if defined(UNIX)
14532# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014533 char **argv = NULL;
14534 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014535#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014536 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014537#endif
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014538 jobopt_T options;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014539
14540 rettv->v_type = VAR_JOB;
14541 job = job_alloc();
14542 rettv->vval.v_job = job;
14543 if (job == NULL)
14544 return;
14545
14546 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014547
14548 /* Default mode is NL. */
14549 options.jo_mode = MODE_NL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010014550 options.jo_callback = NULL;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014551 if (argvars[1].v_type != VAR_UNKNOWN)
14552 {
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014553 if (argvars[1].v_type != VAR_DICT)
14554 {
14555 EMSG(_(e_invarg));
14556 return;
14557 }
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010014558 if (get_job_options(argvars[1].vval.v_dict, &options) == FAIL)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014559 return;
14560 }
14561
Bram Moolenaar835dc632016-02-07 14:27:38 +010014562#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014563 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014564#endif
14565
14566 if (argvars[0].v_type == VAR_STRING)
14567 {
14568 /* Command is a string. */
14569 cmd = argvars[0].vval.v_string;
14570#ifdef USE_ARGV
14571 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14572 return;
14573 argv[argc] = NULL;
14574#endif
14575 }
14576 else if (argvars[0].v_type != VAR_LIST
14577 || argvars[0].vval.v_list == NULL
14578 || argvars[0].vval.v_list->lv_len < 1)
14579 {
14580 EMSG(_(e_invarg));
14581 return;
14582 }
14583 else
14584 {
14585 list_T *l = argvars[0].vval.v_list;
14586 listitem_T *li;
14587 char_u *s;
14588
14589#ifdef USE_ARGV
14590 /* Pass argv[] to mch_call_shell(). */
14591 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14592 if (argv == NULL)
14593 return;
14594#endif
14595 for (li = l->lv_first; li != NULL; li = li->li_next)
14596 {
14597 s = get_tv_string_chk(&li->li_tv);
14598 if (s == NULL)
14599 goto theend;
14600#ifdef USE_ARGV
14601 argv[argc++] = (char *)s;
14602#else
14603 if (li != l->lv_first)
14604 {
14605 s = vim_strsave_shellescape(s, FALSE, TRUE);
14606 if (s == NULL)
14607 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014608 ga_concat(&ga, s);
14609 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014610 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014611 else
14612 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014613 if (li->li_next != NULL)
14614 ga_append(&ga, ' ');
14615#endif
14616 }
14617#ifdef USE_ARGV
14618 argv[argc] = NULL;
14619#else
14620 cmd = ga.ga_data;
14621#endif
14622 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014623
Bram Moolenaar835dc632016-02-07 14:27:38 +010014624#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014625# ifdef FEAT_CHANNEL
14626 if (ch_log_active())
14627 {
14628 garray_T ga;
14629 int i;
14630
14631 ga_init2(&ga, (int)sizeof(char), 200);
14632 for (i = 0; i < argc; ++i)
14633 {
14634 if (i > 0)
14635 ga_concat(&ga, (char_u *)" ");
14636 ga_concat(&ga, (char_u *)argv[i]);
14637 }
14638 ch_logs(NULL, "Starting job: %s", ga.ga_data);
14639 ga_clear(&ga);
14640 }
14641# endif
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014642 mch_start_job(argv, job, &options);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014643#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014644# ifdef FEAT_CHANNEL
14645 ch_logs(NULL, "Starting job: %s", cmd);
14646# endif
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014647 mch_start_job((char *)cmd, job, &options);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014648#endif
14649
14650theend:
14651#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014652 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014653#else
14654 vim_free(ga.ga_data);
14655#endif
14656}
14657
14658/*
14659 * "job_status()" function
14660 */
14661 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014662f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014663{
14664 char *result;
14665
14666 if (argvars[0].v_type != VAR_JOB)
14667 EMSG(_(e_invarg));
14668 else
14669 {
14670 job_T *job = argvars[0].vval.v_job;
14671
14672 if (job->jv_status == JOB_ENDED)
14673 /* No need to check, dead is dead. */
14674 result = "dead";
14675 else if (job->jv_status == JOB_FAILED)
14676 result = "fail";
14677 else
14678 result = mch_job_status(job);
14679 rettv->v_type = VAR_STRING;
14680 rettv->vval.v_string = vim_strsave((char_u *)result);
14681 }
14682}
14683
14684/*
14685 * "job_stop()" function
14686 */
14687 static void
14688f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14689{
14690 if (argvars[0].v_type != VAR_JOB)
14691 EMSG(_(e_invarg));
14692 else
14693 {
14694 char_u *arg;
14695
14696 if (argvars[1].v_type == VAR_UNKNOWN)
14697 arg = (char_u *)"";
14698 else
14699 {
14700 arg = get_tv_string_chk(&argvars[1]);
14701 if (arg == NULL)
14702 {
14703 EMSG(_(e_invarg));
14704 return;
14705 }
14706 }
14707 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14708 rettv->vval.v_number = 0;
14709 else
14710 rettv->vval.v_number = 1;
14711 }
14712}
14713#endif
14714
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014716 * "join()" function
14717 */
14718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014719f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014720{
14721 garray_T ga;
14722 char_u *sep;
14723
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014724 if (argvars[0].v_type != VAR_LIST)
14725 {
14726 EMSG(_(e_listreq));
14727 return;
14728 }
14729 if (argvars[0].vval.v_list == NULL)
14730 return;
14731 if (argvars[1].v_type == VAR_UNKNOWN)
14732 sep = (char_u *)" ";
14733 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014734 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014735
14736 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014737
14738 if (sep != NULL)
14739 {
14740 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014741 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 ga_append(&ga, NUL);
14743 rettv->vval.v_string = (char_u *)ga.ga_data;
14744 }
14745 else
14746 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014747}
14748
14749/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014750 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014751 */
14752 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014753f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014754{
14755 js_read_T reader;
14756
14757 reader.js_buf = get_tv_string(&argvars[0]);
14758 reader.js_fill = NULL;
14759 reader.js_used = 0;
14760 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14761 EMSG(_(e_invarg));
14762}
14763
14764/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014765 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014766 */
14767 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014768f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014769{
14770 rettv->v_type = VAR_STRING;
14771 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14772}
14773
14774/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014775 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014776 */
14777 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014778f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014779{
14780 js_read_T reader;
14781
14782 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014783 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014784 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014785 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014786 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014787}
14788
14789/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014790 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014791 */
14792 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014793f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014794{
14795 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014796 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014797}
14798
14799/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014800 * "keys()" function
14801 */
14802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014803f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014804{
14805 dict_list(argvars, rettv, 0);
14806}
14807
14808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 * "last_buffer_nr()" function.
14810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014812f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813{
14814 int n = 0;
14815 buf_T *buf;
14816
14817 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14818 if (n < buf->b_fnum)
14819 n = buf->b_fnum;
14820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014821 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014822}
14823
14824/*
14825 * "len()" function
14826 */
14827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014828f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014829{
14830 switch (argvars[0].v_type)
14831 {
14832 case VAR_STRING:
14833 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014834 rettv->vval.v_number = (varnumber_T)STRLEN(
14835 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014836 break;
14837 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014838 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014839 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014840 case VAR_DICT:
14841 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14842 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014843 case VAR_UNKNOWN:
14844 case VAR_SPECIAL:
14845 case VAR_FLOAT:
14846 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014847 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014848 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014849 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014850 break;
14851 }
14852}
14853
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014854static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014855
14856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014857libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014858{
14859#ifdef FEAT_LIBCALL
14860 char_u *string_in;
14861 char_u **string_result;
14862 int nr_result;
14863#endif
14864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014865 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014866 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014867 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014868
14869 if (check_restricted() || check_secure())
14870 return;
14871
14872#ifdef FEAT_LIBCALL
14873 /* The first two args must be strings, otherwise its meaningless */
14874 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14875 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014876 string_in = NULL;
14877 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014878 string_in = argvars[2].vval.v_string;
14879 if (type == VAR_NUMBER)
14880 string_result = NULL;
14881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014882 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014883 if (mch_libcall(argvars[0].vval.v_string,
14884 argvars[1].vval.v_string,
14885 string_in,
14886 argvars[2].vval.v_number,
14887 string_result,
14888 &nr_result) == OK
14889 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014890 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014891 }
14892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893}
14894
14895/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014896 * "libcall()" function
14897 */
14898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014899f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014900{
14901 libcall_common(argvars, rettv, VAR_STRING);
14902}
14903
14904/*
14905 * "libcallnr()" function
14906 */
14907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014908f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014909{
14910 libcall_common(argvars, rettv, VAR_NUMBER);
14911}
14912
14913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 * "line(string)" function
14915 */
14916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014917f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918{
14919 linenr_T lnum = 0;
14920 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014921 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014923 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924 if (fp != NULL)
14925 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014926 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927}
14928
14929/*
14930 * "line2byte(lnum)" function
14931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014933f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934{
14935#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014936 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937#else
14938 linenr_T lnum;
14939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014940 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014942 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014944 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14945 if (rettv->vval.v_number >= 0)
14946 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947#endif
14948}
14949
14950/*
14951 * "lispindent(lnum)" function
14952 */
14953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014954f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955{
14956#ifdef FEAT_LISP
14957 pos_T pos;
14958 linenr_T lnum;
14959
14960 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14963 {
14964 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014965 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 curwin->w_cursor = pos;
14967 }
14968 else
14969#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014970 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971}
14972
14973/*
14974 * "localtime()" function
14975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014977f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014979 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980}
14981
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014982static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983
14984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014985get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986{
14987 char_u *keys;
14988 char_u *which;
14989 char_u buf[NUMBUFLEN];
14990 char_u *keys_buf = NULL;
14991 char_u *rhs;
14992 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014993 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014994 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014995 mapblock_T *mp;
14996 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997
14998 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014999 rettv->v_type = VAR_STRING;
15000 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015002 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003 if (*keys == NUL)
15004 return;
15005
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015006 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015007 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015008 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015009 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015010 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015011 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015012 if (argvars[3].v_type != VAR_UNKNOWN)
15013 get_dict = get_tv_number(&argvars[3]);
15014 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 else
15017 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015018 if (which == NULL)
15019 return;
15020
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 mode = get_map_mode(&which, 0);
15022
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015023 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015024 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015026
15027 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015029 /* Return a string. */
15030 if (rhs != NULL)
15031 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032
Bram Moolenaarbd743252010-10-20 21:23:33 +020015033 }
15034 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15035 {
15036 /* Return a dictionary. */
15037 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15038 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15039 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040
Bram Moolenaarbd743252010-10-20 21:23:33 +020015041 dict_add_nr_str(dict, "lhs", 0L, lhs);
15042 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15043 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15044 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15045 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15046 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15047 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015048 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015049 dict_add_nr_str(dict, "mode", 0L, mapmode);
15050
15051 vim_free(lhs);
15052 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053 }
15054}
15055
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015056#ifdef FEAT_FLOAT
15057/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015058 * "log()" function
15059 */
15060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015061f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015062{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015063 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015064
15065 rettv->v_type = VAR_FLOAT;
15066 if (get_float_arg(argvars, &f) == OK)
15067 rettv->vval.v_float = log(f);
15068 else
15069 rettv->vval.v_float = 0.0;
15070}
15071
15072/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015073 * "log10()" function
15074 */
15075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015076f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015077{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015078 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015079
15080 rettv->v_type = VAR_FLOAT;
15081 if (get_float_arg(argvars, &f) == OK)
15082 rettv->vval.v_float = log10(f);
15083 else
15084 rettv->vval.v_float = 0.0;
15085}
15086#endif
15087
Bram Moolenaar1dced572012-04-05 16:54:08 +020015088#ifdef FEAT_LUA
15089/*
15090 * "luaeval()" function
15091 */
15092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015093f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015094{
15095 char_u *str;
15096 char_u buf[NUMBUFLEN];
15097
15098 str = get_tv_string_buf(&argvars[0], buf);
15099 do_luaeval(str, argvars + 1, rettv);
15100}
15101#endif
15102
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015104 * "map()" function
15105 */
15106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015107f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015108{
15109 filter_map(argvars, rettv, TRUE);
15110}
15111
15112/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 */
15115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015116f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015118 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119}
15120
15121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123 */
15124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015125f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128}
15129
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015130static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131
15132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015133find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015135 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015136 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015137 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 char_u *pat;
15139 regmatch_T regmatch;
15140 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015141 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142 char_u *save_cpo;
15143 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015144 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015145 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015146 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 list_T *l = NULL;
15148 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015149 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015150 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151
15152 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15153 save_cpo = p_cpo;
15154 p_cpo = (char_u *)"";
15155
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015156 rettv->vval.v_number = -1;
15157 if (type == 3)
15158 {
15159 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015160 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015161 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015162 }
15163 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165 rettv->v_type = VAR_STRING;
15166 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015169 if (argvars[0].v_type == VAR_LIST)
15170 {
15171 if ((l = argvars[0].vval.v_list) == NULL)
15172 goto theend;
15173 li = l->lv_first;
15174 }
15175 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015176 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015177 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015178 len = (long)STRLEN(str);
15179 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015180
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015181 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15182 if (pat == NULL)
15183 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015184
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015185 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015187 int error = FALSE;
15188
15189 start = get_tv_number_chk(&argvars[2], &error);
15190 if (error)
15191 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015192 if (l != NULL)
15193 {
15194 li = list_find(l, start);
15195 if (li == NULL)
15196 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015197 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015198 }
15199 else
15200 {
15201 if (start < 0)
15202 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015203 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015204 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015205 /* When "count" argument is there ignore matches before "start",
15206 * otherwise skip part of the string. Differs when pattern is "^"
15207 * or "\<". */
15208 if (argvars[3].v_type != VAR_UNKNOWN)
15209 startcol = start;
15210 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015211 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015212 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015213 len -= start;
15214 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015215 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015216
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015217 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015218 nth = get_tv_number_chk(&argvars[3], &error);
15219 if (error)
15220 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 }
15222
15223 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15224 if (regmatch.regprog != NULL)
15225 {
15226 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015227
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015228 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015229 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015230 if (l != NULL)
15231 {
15232 if (li == NULL)
15233 {
15234 match = FALSE;
15235 break;
15236 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015237 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015238 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015239 if (str == NULL)
15240 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015241 }
15242
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015243 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015244
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015245 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015246 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015247 if (l == NULL && !match)
15248 break;
15249
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015250 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015251 if (l != NULL)
15252 {
15253 li = li->li_next;
15254 ++idx;
15255 }
15256 else
15257 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015258#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015259 startcol = (colnr_T)(regmatch.startp[0]
15260 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015261#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015262 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015263#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015264 if (startcol > (colnr_T)len
15265 || str + startcol <= regmatch.startp[0])
15266 {
15267 match = FALSE;
15268 break;
15269 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015270 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015271 }
15272
15273 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015275 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015277 int i;
15278
15279 /* return list with matched string and submatches */
15280 for (i = 0; i < NSUBEXP; ++i)
15281 {
15282 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015283 {
15284 if (list_append_string(rettv->vval.v_list,
15285 (char_u *)"", 0) == FAIL)
15286 break;
15287 }
15288 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015289 regmatch.startp[i],
15290 (int)(regmatch.endp[i] - regmatch.startp[i]))
15291 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015292 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015293 }
15294 }
15295 else if (type == 2)
15296 {
15297 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015298 if (l != NULL)
15299 copy_tv(&li->li_tv, rettv);
15300 else
15301 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015303 }
15304 else if (l != NULL)
15305 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 else
15307 {
15308 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015309 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310 (varnumber_T)(regmatch.startp[0] - str);
15311 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015312 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015314 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 }
15316 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015317 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 }
15319
15320theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015321 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 p_cpo = save_cpo;
15323}
15324
15325/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015326 * "match()" function
15327 */
15328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015329f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015330{
15331 find_some_match(argvars, rettv, 1);
15332}
15333
15334/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015335 * "matchadd()" function
15336 */
15337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015338f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015339{
15340#ifdef FEAT_SEARCH_EXTRA
15341 char_u buf[NUMBUFLEN];
15342 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15343 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15344 int prio = 10; /* default priority */
15345 int id = -1;
15346 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015347 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015348
15349 rettv->vval.v_number = -1;
15350
15351 if (grp == NULL || pat == NULL)
15352 return;
15353 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015354 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015355 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015356 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015357 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015358 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015359 if (argvars[4].v_type != VAR_UNKNOWN)
15360 {
15361 if (argvars[4].v_type != VAR_DICT)
15362 {
15363 EMSG(_(e_dictreq));
15364 return;
15365 }
15366 if (dict_find(argvars[4].vval.v_dict,
15367 (char_u *)"conceal", -1) != NULL)
15368 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15369 (char_u *)"conceal", FALSE);
15370 }
15371 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015372 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015373 if (error == TRUE)
15374 return;
15375 if (id >= 1 && id <= 3)
15376 {
15377 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15378 return;
15379 }
15380
Bram Moolenaar6561d522015-07-21 15:48:27 +020015381 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15382 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015383#endif
15384}
15385
15386/*
15387 * "matchaddpos()" function
15388 */
15389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015390f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015391{
15392#ifdef FEAT_SEARCH_EXTRA
15393 char_u buf[NUMBUFLEN];
15394 char_u *group;
15395 int prio = 10;
15396 int id = -1;
15397 int error = FALSE;
15398 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015399 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015400
15401 rettv->vval.v_number = -1;
15402
15403 group = get_tv_string_buf_chk(&argvars[0], buf);
15404 if (group == NULL)
15405 return;
15406
15407 if (argvars[1].v_type != VAR_LIST)
15408 {
15409 EMSG2(_(e_listarg), "matchaddpos()");
15410 return;
15411 }
15412 l = argvars[1].vval.v_list;
15413 if (l == NULL)
15414 return;
15415
15416 if (argvars[2].v_type != VAR_UNKNOWN)
15417 {
15418 prio = get_tv_number_chk(&argvars[2], &error);
15419 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015420 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015421 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015422 if (argvars[4].v_type != VAR_UNKNOWN)
15423 {
15424 if (argvars[4].v_type != VAR_DICT)
15425 {
15426 EMSG(_(e_dictreq));
15427 return;
15428 }
15429 if (dict_find(argvars[4].vval.v_dict,
15430 (char_u *)"conceal", -1) != NULL)
15431 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15432 (char_u *)"conceal", FALSE);
15433 }
15434 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015435 }
15436 if (error == TRUE)
15437 return;
15438
15439 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15440 if (id == 1 || id == 2)
15441 {
15442 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15443 return;
15444 }
15445
Bram Moolenaar6561d522015-07-21 15:48:27 +020015446 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15447 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015448#endif
15449}
15450
15451/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015452 * "matcharg()" function
15453 */
15454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015455f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015456{
15457 if (rettv_list_alloc(rettv) == OK)
15458 {
15459#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015460 int id = get_tv_number(&argvars[0]);
15461 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015462
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015463 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015464 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015465 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15466 {
15467 list_append_string(rettv->vval.v_list,
15468 syn_id2name(m->hlg_id), -1);
15469 list_append_string(rettv->vval.v_list, m->pattern, -1);
15470 }
15471 else
15472 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015473 list_append_string(rettv->vval.v_list, NULL, -1);
15474 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015475 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015476 }
15477#endif
15478 }
15479}
15480
15481/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015482 * "matchdelete()" function
15483 */
15484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015485f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015486{
15487#ifdef FEAT_SEARCH_EXTRA
15488 rettv->vval.v_number = match_delete(curwin,
15489 (int)get_tv_number(&argvars[0]), TRUE);
15490#endif
15491}
15492
15493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015494 * "matchend()" function
15495 */
15496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015497f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015498{
15499 find_some_match(argvars, rettv, 0);
15500}
15501
15502/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015503 * "matchlist()" function
15504 */
15505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015506f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015507{
15508 find_some_match(argvars, rettv, 3);
15509}
15510
15511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015512 * "matchstr()" function
15513 */
15514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015515f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015516{
15517 find_some_match(argvars, rettv, 2);
15518}
15519
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015520static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015521
15522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015523max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015524{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015525 long n = 0;
15526 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015527 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015528
15529 if (argvars[0].v_type == VAR_LIST)
15530 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015531 list_T *l;
15532 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015533
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015534 l = argvars[0].vval.v_list;
15535 if (l != NULL)
15536 {
15537 li = l->lv_first;
15538 if (li != NULL)
15539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015540 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015541 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015542 {
15543 li = li->li_next;
15544 if (li == NULL)
15545 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015546 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015547 if (domax ? i > n : i < n)
15548 n = i;
15549 }
15550 }
15551 }
15552 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015553 else if (argvars[0].v_type == VAR_DICT)
15554 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015555 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015556 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015557 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015558 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015559
15560 d = argvars[0].vval.v_dict;
15561 if (d != NULL)
15562 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015563 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015564 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015565 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015566 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015567 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015568 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015569 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015570 if (first)
15571 {
15572 n = i;
15573 first = FALSE;
15574 }
15575 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015576 n = i;
15577 }
15578 }
15579 }
15580 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015581 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015582 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015584}
15585
15586/*
15587 * "max()" function
15588 */
15589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015590f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015591{
15592 max_min(argvars, rettv, TRUE);
15593}
15594
15595/*
15596 * "min()" function
15597 */
15598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015599f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015600{
15601 max_min(argvars, rettv, FALSE);
15602}
15603
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015604static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015605
15606/*
15607 * Create the directory in which "dir" is located, and higher levels when
15608 * needed.
15609 */
15610 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015611mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015612{
15613 char_u *p;
15614 char_u *updir;
15615 int r = FAIL;
15616
15617 /* Get end of directory name in "dir".
15618 * We're done when it's "/" or "c:/". */
15619 p = gettail_sep(dir);
15620 if (p <= get_past_head(dir))
15621 return OK;
15622
15623 /* If the directory exists we're done. Otherwise: create it.*/
15624 updir = vim_strnsave(dir, (int)(p - dir));
15625 if (updir == NULL)
15626 return FAIL;
15627 if (mch_isdir(updir))
15628 r = OK;
15629 else if (mkdir_recurse(updir, prot) == OK)
15630 r = vim_mkdir_emsg(updir, prot);
15631 vim_free(updir);
15632 return r;
15633}
15634
15635#ifdef vim_mkdir
15636/*
15637 * "mkdir()" function
15638 */
15639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015640f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015641{
15642 char_u *dir;
15643 char_u buf[NUMBUFLEN];
15644 int prot = 0755;
15645
15646 rettv->vval.v_number = FAIL;
15647 if (check_restricted() || check_secure())
15648 return;
15649
15650 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015651 if (*dir == NUL)
15652 rettv->vval.v_number = FAIL;
15653 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015654 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015655 if (*gettail(dir) == NUL)
15656 /* remove trailing slashes */
15657 *gettail_sep(dir) = NUL;
15658
15659 if (argvars[1].v_type != VAR_UNKNOWN)
15660 {
15661 if (argvars[2].v_type != VAR_UNKNOWN)
15662 prot = get_tv_number_chk(&argvars[2], NULL);
15663 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15664 mkdir_recurse(dir, prot);
15665 }
15666 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015667 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015668}
15669#endif
15670
Bram Moolenaar0d660222005-01-07 21:51:51 +000015671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 * "mode()" function
15673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015675f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015677 char_u buf[3];
15678
15679 buf[1] = NUL;
15680 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 if (VIsual_active)
15683 {
15684 if (VIsual_select)
15685 buf[0] = VIsual_mode + 's' - 'v';
15686 else
15687 buf[0] = VIsual_mode;
15688 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015689 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015690 || State == CONFIRM)
15691 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015693 if (State == ASKMORE)
15694 buf[1] = 'm';
15695 else if (State == CONFIRM)
15696 buf[1] = '?';
15697 }
15698 else if (State == EXTERNCMD)
15699 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700 else if (State & INSERT)
15701 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015702#ifdef FEAT_VREPLACE
15703 if (State & VREPLACE_FLAG)
15704 {
15705 buf[0] = 'R';
15706 buf[1] = 'v';
15707 }
15708 else
15709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 if (State & REPLACE_FLAG)
15711 buf[0] = 'R';
15712 else
15713 buf[0] = 'i';
15714 }
15715 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015718 if (exmode_active)
15719 buf[1] = 'v';
15720 }
15721 else if (exmode_active)
15722 {
15723 buf[0] = 'c';
15724 buf[1] = 'e';
15725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015729 if (finish_op)
15730 buf[1] = 'o';
15731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015733 /* Clear out the minor mode when the argument is not a non-zero number or
15734 * non-empty string. */
15735 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015736 buf[1] = NUL;
15737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 rettv->vval.v_string = vim_strsave(buf);
15739 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740}
15741
Bram Moolenaar429fa852013-04-15 12:27:36 +020015742#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015743/*
15744 * "mzeval()" function
15745 */
15746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015747f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015748{
15749 char_u *str;
15750 char_u buf[NUMBUFLEN];
15751
15752 str = get_tv_string_buf(&argvars[0], buf);
15753 do_mzeval(str, rettv);
15754}
Bram Moolenaar75676462013-01-30 14:55:42 +010015755
15756 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015757mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015758{
15759 typval_T argvars[3];
15760
15761 argvars[0].v_type = VAR_STRING;
15762 argvars[0].vval.v_string = name;
15763 copy_tv(args, &argvars[1]);
15764 argvars[2].v_type = VAR_UNKNOWN;
15765 f_call(argvars, rettv);
15766 clear_tv(&argvars[1]);
15767}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015768#endif
15769
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015771 * "nextnonblank()" function
15772 */
15773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015774f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015775{
15776 linenr_T lnum;
15777
15778 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015780 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015781 {
15782 lnum = 0;
15783 break;
15784 }
15785 if (*skipwhite(ml_get(lnum)) != NUL)
15786 break;
15787 }
15788 rettv->vval.v_number = lnum;
15789}
15790
15791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 * "nr2char()" function
15793 */
15794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015795f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796{
15797 char_u buf[NUMBUFLEN];
15798
15799#ifdef FEAT_MBYTE
15800 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015801 {
15802 int utf8 = 0;
15803
15804 if (argvars[1].v_type != VAR_UNKNOWN)
15805 utf8 = get_tv_number_chk(&argvars[1], NULL);
15806 if (utf8)
15807 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15808 else
15809 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811 else
15812#endif
15813 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015814 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 buf[1] = NUL;
15816 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015817 rettv->v_type = VAR_STRING;
15818 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015819}
15820
15821/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015822 * "or(expr, expr)" function
15823 */
15824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015825f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015826{
15827 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15828 | get_tv_number_chk(&argvars[1], NULL);
15829}
15830
15831/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015832 * "pathshorten()" function
15833 */
15834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015835f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015836{
15837 char_u *p;
15838
15839 rettv->v_type = VAR_STRING;
15840 p = get_tv_string_chk(&argvars[0]);
15841 if (p == NULL)
15842 rettv->vval.v_string = NULL;
15843 else
15844 {
15845 p = vim_strsave(p);
15846 rettv->vval.v_string = p;
15847 if (p != NULL)
15848 shorten_dir(p);
15849 }
15850}
15851
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015852#ifdef FEAT_PERL
15853/*
15854 * "perleval()" function
15855 */
15856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015857f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015858{
15859 char_u *str;
15860 char_u buf[NUMBUFLEN];
15861
15862 str = get_tv_string_buf(&argvars[0], buf);
15863 do_perleval(str, rettv);
15864}
15865#endif
15866
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015867#ifdef FEAT_FLOAT
15868/*
15869 * "pow()" function
15870 */
15871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015872f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015873{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015874 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015875
15876 rettv->v_type = VAR_FLOAT;
15877 if (get_float_arg(argvars, &fx) == OK
15878 && get_float_arg(&argvars[1], &fy) == OK)
15879 rettv->vval.v_float = pow(fx, fy);
15880 else
15881 rettv->vval.v_float = 0.0;
15882}
15883#endif
15884
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015885/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015886 * "prevnonblank()" function
15887 */
15888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015889f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015890{
15891 linenr_T lnum;
15892
15893 lnum = get_tv_lnum(argvars);
15894 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15895 lnum = 0;
15896 else
15897 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15898 --lnum;
15899 rettv->vval.v_number = lnum;
15900}
15901
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015902/* This dummy va_list is here because:
15903 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15904 * - locally in the function results in a "used before set" warning
15905 * - using va_start() to initialize it gives "function with fixed args" error */
15906static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015907
Bram Moolenaar8c711452005-01-14 21:53:12 +000015908/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015909 * "printf()" function
15910 */
15911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015912f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015913{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015914 char_u buf[NUMBUFLEN];
15915 int len;
15916 char_u *s;
15917 int saved_did_emsg = did_emsg;
15918 char *fmt;
15919
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015920 rettv->v_type = VAR_STRING;
15921 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015922
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015923 /* Get the required length, allocate the buffer and do it for real. */
15924 did_emsg = FALSE;
15925 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15926 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15927 if (!did_emsg)
15928 {
15929 s = alloc(len + 1);
15930 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015931 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015932 rettv->vval.v_string = s;
15933 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015934 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015935 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015936 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015937}
15938
15939/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015940 * "pumvisible()" function
15941 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015943f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015944{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015945#ifdef FEAT_INS_EXPAND
15946 if (pum_visible())
15947 rettv->vval.v_number = 1;
15948#endif
15949}
15950
Bram Moolenaardb913952012-06-29 12:54:53 +020015951#ifdef FEAT_PYTHON3
15952/*
15953 * "py3eval()" function
15954 */
15955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015956f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015957{
15958 char_u *str;
15959 char_u buf[NUMBUFLEN];
15960
15961 str = get_tv_string_buf(&argvars[0], buf);
15962 do_py3eval(str, rettv);
15963}
15964#endif
15965
15966#ifdef FEAT_PYTHON
15967/*
15968 * "pyeval()" function
15969 */
15970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015971f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015972{
15973 char_u *str;
15974 char_u buf[NUMBUFLEN];
15975
15976 str = get_tv_string_buf(&argvars[0], buf);
15977 do_pyeval(str, rettv);
15978}
15979#endif
15980
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015981/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015982 * "range()" function
15983 */
15984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015985f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015986{
15987 long start;
15988 long end;
15989 long stride = 1;
15990 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015991 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015993 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015994 if (argvars[1].v_type == VAR_UNKNOWN)
15995 {
15996 end = start - 1;
15997 start = 0;
15998 }
15999 else
16000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016001 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016002 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016003 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016004 }
16005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016006 if (error)
16007 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016008 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016009 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016010 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016011 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016012 else
16013 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016014 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016015 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016016 if (list_append_number(rettv->vval.v_list,
16017 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016018 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016019 }
16020}
16021
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016022/*
16023 * "readfile()" function
16024 */
16025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016026f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016027{
16028 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016029 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016030 char_u *fname;
16031 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016032 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16033 int io_size = sizeof(buf);
16034 int readlen; /* size of last fread() */
16035 char_u *prev = NULL; /* previously read bytes, if any */
16036 long prevlen = 0; /* length of data in prev */
16037 long prevsize = 0; /* size of prev buffer */
16038 long maxline = MAXLNUM;
16039 long cnt = 0;
16040 char_u *p; /* position in buf */
16041 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016042
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016043 if (argvars[1].v_type != VAR_UNKNOWN)
16044 {
16045 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16046 binary = TRUE;
16047 if (argvars[2].v_type != VAR_UNKNOWN)
16048 maxline = get_tv_number(&argvars[2]);
16049 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016050
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016051 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016052 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016053
16054 /* Always open the file in binary mode, library functions have a mind of
16055 * their own about CR-LF conversion. */
16056 fname = get_tv_string(&argvars[0]);
16057 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16058 {
16059 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16060 return;
16061 }
16062
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016063 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016064 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016065 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016066
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016067 /* This for loop processes what was read, but is also entered at end
16068 * of file so that either:
16069 * - an incomplete line gets written
16070 * - a "binary" file gets an empty line at the end if it ends in a
16071 * newline. */
16072 for (p = buf, start = buf;
16073 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16074 ++p)
16075 {
16076 if (*p == '\n' || readlen <= 0)
16077 {
16078 listitem_T *li;
16079 char_u *s = NULL;
16080 long_u len = p - start;
16081
16082 /* Finished a line. Remove CRs before NL. */
16083 if (readlen > 0 && !binary)
16084 {
16085 while (len > 0 && start[len - 1] == '\r')
16086 --len;
16087 /* removal may cross back to the "prev" string */
16088 if (len == 0)
16089 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16090 --prevlen;
16091 }
16092 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016093 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016094 else
16095 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016096 /* Change "prev" buffer to be the right size. This way
16097 * the bytes are only copied once, and very long lines are
16098 * allocated only once. */
16099 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016100 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016101 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016102 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016103 prev = NULL; /* the list will own the string */
16104 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016105 }
16106 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016107 if (s == NULL)
16108 {
16109 do_outofmem_msg((long_u) prevlen + len + 1);
16110 failed = TRUE;
16111 break;
16112 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016113
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016114 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016115 {
16116 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016117 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016118 break;
16119 }
16120 li->li_tv.v_type = VAR_STRING;
16121 li->li_tv.v_lock = 0;
16122 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016123 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016124
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016125 start = p + 1; /* step over newline */
16126 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016127 break;
16128 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016129 else if (*p == NUL)
16130 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016131#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016132 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16133 * when finding the BF and check the previous two bytes. */
16134 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016135 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016136 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16137 * + 1, these may be in the "prev" string. */
16138 char_u back1 = p >= buf + 1 ? p[-1]
16139 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16140 char_u back2 = p >= buf + 2 ? p[-2]
16141 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16142 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016143
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016144 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016145 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016146 char_u *dest = p - 2;
16147
16148 /* Usually a BOM is at the beginning of a file, and so at
16149 * the beginning of a line; then we can just step over it.
16150 */
16151 if (start == dest)
16152 start = p + 1;
16153 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016154 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016155 /* have to shuffle buf to close gap */
16156 int adjust_prevlen = 0;
16157
16158 if (dest < buf)
16159 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016160 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016161 dest = buf;
16162 }
16163 if (readlen > p - buf + 1)
16164 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16165 readlen -= 3 - adjust_prevlen;
16166 prevlen -= adjust_prevlen;
16167 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016168 }
16169 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016170 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016171#endif
16172 } /* for */
16173
16174 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16175 break;
16176 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016177 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016178 /* There's part of a line in buf, store it in "prev". */
16179 if (p - start + prevlen >= prevsize)
16180 {
16181 /* need bigger "prev" buffer */
16182 char_u *newprev;
16183
16184 /* A common use case is ordinary text files and "prev" gets a
16185 * fragment of a line, so the first allocation is made
16186 * small, to avoid repeatedly 'allocing' large and
16187 * 'reallocing' small. */
16188 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016189 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016190 else
16191 {
16192 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016193 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016194 prevsize = grow50pc > growmin ? grow50pc : growmin;
16195 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016196 newprev = prev == NULL ? alloc(prevsize)
16197 : vim_realloc(prev, prevsize);
16198 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016199 {
16200 do_outofmem_msg((long_u)prevsize);
16201 failed = TRUE;
16202 break;
16203 }
16204 prev = newprev;
16205 }
16206 /* Add the line part to end of "prev". */
16207 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016208 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016209 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016210 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016211
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016212 /*
16213 * For a negative line count use only the lines at the end of the file,
16214 * free the rest.
16215 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016216 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016217 while (cnt > -maxline)
16218 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016219 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016220 --cnt;
16221 }
16222
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016223 if (failed)
16224 {
16225 list_free(rettv->vval.v_list, TRUE);
16226 /* readfile doc says an empty list is returned on error */
16227 rettv->vval.v_list = list_alloc();
16228 }
16229
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016230 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016231 fclose(fd);
16232}
16233
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016234#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016235static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016236
16237/*
16238 * Convert a List to proftime_T.
16239 * Return FAIL when there is something wrong.
16240 */
16241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016242list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016243{
16244 long n1, n2;
16245 int error = FALSE;
16246
16247 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16248 || arg->vval.v_list->lv_len != 2)
16249 return FAIL;
16250 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16251 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16252# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016253 tm->HighPart = n1;
16254 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016255# else
16256 tm->tv_sec = n1;
16257 tm->tv_usec = n2;
16258# endif
16259 return error ? FAIL : OK;
16260}
16261#endif /* FEAT_RELTIME */
16262
16263/*
16264 * "reltime()" function
16265 */
16266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016267f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016268{
16269#ifdef FEAT_RELTIME
16270 proftime_T res;
16271 proftime_T start;
16272
16273 if (argvars[0].v_type == VAR_UNKNOWN)
16274 {
16275 /* No arguments: get current time. */
16276 profile_start(&res);
16277 }
16278 else if (argvars[1].v_type == VAR_UNKNOWN)
16279 {
16280 if (list2proftime(&argvars[0], &res) == FAIL)
16281 return;
16282 profile_end(&res);
16283 }
16284 else
16285 {
16286 /* Two arguments: compute the difference. */
16287 if (list2proftime(&argvars[0], &start) == FAIL
16288 || list2proftime(&argvars[1], &res) == FAIL)
16289 return;
16290 profile_sub(&res, &start);
16291 }
16292
16293 if (rettv_list_alloc(rettv) == OK)
16294 {
16295 long n1, n2;
16296
16297# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016298 n1 = res.HighPart;
16299 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016300# else
16301 n1 = res.tv_sec;
16302 n2 = res.tv_usec;
16303# endif
16304 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16305 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16306 }
16307#endif
16308}
16309
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016310#ifdef FEAT_FLOAT
16311/*
16312 * "reltimefloat()" function
16313 */
16314 static void
16315f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16316{
16317# ifdef FEAT_RELTIME
16318 proftime_T tm;
16319# endif
16320
16321 rettv->v_type = VAR_FLOAT;
16322 rettv->vval.v_float = 0;
16323# ifdef FEAT_RELTIME
16324 if (list2proftime(&argvars[0], &tm) == OK)
16325 rettv->vval.v_float = profile_float(&tm);
16326# endif
16327}
16328#endif
16329
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016330/*
16331 * "reltimestr()" function
16332 */
16333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016334f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016335{
16336#ifdef FEAT_RELTIME
16337 proftime_T tm;
16338#endif
16339
16340 rettv->v_type = VAR_STRING;
16341 rettv->vval.v_string = NULL;
16342#ifdef FEAT_RELTIME
16343 if (list2proftime(&argvars[0], &tm) == OK)
16344 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16345#endif
16346}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016347
Bram Moolenaar0d660222005-01-07 21:51:51 +000016348#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016349static void make_connection(void);
16350static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351
16352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016353make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354{
16355 if (X_DISPLAY == NULL
16356# ifdef FEAT_GUI
16357 && !gui.in_use
16358# endif
16359 )
16360 {
16361 x_force_connect = TRUE;
16362 setup_term_clip();
16363 x_force_connect = FALSE;
16364 }
16365}
16366
16367 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016368check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369{
16370 make_connection();
16371 if (X_DISPLAY == NULL)
16372 {
16373 EMSG(_("E240: No connection to Vim server"));
16374 return FAIL;
16375 }
16376 return OK;
16377}
16378#endif
16379
16380#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016381static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382
16383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016384remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016385{
16386 char_u *server_name;
16387 char_u *keys;
16388 char_u *r = NULL;
16389 char_u buf[NUMBUFLEN];
16390# ifdef WIN32
16391 HWND w;
16392# else
16393 Window w;
16394# endif
16395
16396 if (check_restricted() || check_secure())
16397 return;
16398
16399# ifdef FEAT_X11
16400 if (check_connection() == FAIL)
16401 return;
16402# endif
16403
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016404 server_name = get_tv_string_chk(&argvars[0]);
16405 if (server_name == NULL)
16406 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016407 keys = get_tv_string_buf(&argvars[1], buf);
16408# ifdef WIN32
16409 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16410# else
16411 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16412 < 0)
16413# endif
16414 {
16415 if (r != NULL)
16416 EMSG(r); /* sending worked but evaluation failed */
16417 else
16418 EMSG2(_("E241: Unable to send to %s"), server_name);
16419 return;
16420 }
16421
16422 rettv->vval.v_string = r;
16423
16424 if (argvars[2].v_type != VAR_UNKNOWN)
16425 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016426 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016427 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016428 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016429
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016430 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016431 v.di_tv.v_type = VAR_STRING;
16432 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016433 idvar = get_tv_string_chk(&argvars[2]);
16434 if (idvar != NULL)
16435 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016436 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437 }
16438}
16439#endif
16440
16441/*
16442 * "remote_expr()" function
16443 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016445f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016446{
16447 rettv->v_type = VAR_STRING;
16448 rettv->vval.v_string = NULL;
16449#ifdef FEAT_CLIENTSERVER
16450 remote_common(argvars, rettv, TRUE);
16451#endif
16452}
16453
16454/*
16455 * "remote_foreground()" function
16456 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016458f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016460#ifdef FEAT_CLIENTSERVER
16461# ifdef WIN32
16462 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016463 {
16464 char_u *server_name = get_tv_string_chk(&argvars[0]);
16465
16466 if (server_name != NULL)
16467 serverForeground(server_name);
16468 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016469# else
16470 /* Send a foreground() expression to the server. */
16471 argvars[1].v_type = VAR_STRING;
16472 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16473 argvars[2].v_type = VAR_UNKNOWN;
16474 remote_common(argvars, rettv, TRUE);
16475 vim_free(argvars[1].vval.v_string);
16476# endif
16477#endif
16478}
16479
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016481f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016482{
16483#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016484 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016485 char_u *s = NULL;
16486# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016487 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016488# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016489 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016490
16491 if (check_restricted() || check_secure())
16492 {
16493 rettv->vval.v_number = -1;
16494 return;
16495 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016496 serverid = get_tv_string_chk(&argvars[0]);
16497 if (serverid == NULL)
16498 {
16499 rettv->vval.v_number = -1;
16500 return; /* type error; errmsg already given */
16501 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016502# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016503 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016504 if (n == 0)
16505 rettv->vval.v_number = -1;
16506 else
16507 {
16508 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16509 rettv->vval.v_number = (s != NULL);
16510 }
16511# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512 if (check_connection() == FAIL)
16513 return;
16514
16515 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016516 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517# endif
16518
16519 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016521 char_u *retvar;
16522
Bram Moolenaar33570922005-01-25 22:26:29 +000016523 v.di_tv.v_type = VAR_STRING;
16524 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016525 retvar = get_tv_string_chk(&argvars[1]);
16526 if (retvar != NULL)
16527 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016528 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016529 }
16530#else
16531 rettv->vval.v_number = -1;
16532#endif
16533}
16534
Bram Moolenaar0d660222005-01-07 21:51:51 +000016535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016536f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016537{
16538 char_u *r = NULL;
16539
16540#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016541 char_u *serverid = get_tv_string_chk(&argvars[0]);
16542
16543 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016544 {
16545# ifdef WIN32
16546 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016547 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016548
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016549 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016550 if (n != 0)
16551 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16552 if (r == NULL)
16553# else
16554 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016555 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016556# endif
16557 EMSG(_("E277: Unable to read a server reply"));
16558 }
16559#endif
16560 rettv->v_type = VAR_STRING;
16561 rettv->vval.v_string = r;
16562}
16563
16564/*
16565 * "remote_send()" function
16566 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016568f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016569{
16570 rettv->v_type = VAR_STRING;
16571 rettv->vval.v_string = NULL;
16572#ifdef FEAT_CLIENTSERVER
16573 remote_common(argvars, rettv, FALSE);
16574#endif
16575}
16576
16577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016578 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016579 */
16580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016581f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016582{
Bram Moolenaar33570922005-01-25 22:26:29 +000016583 list_T *l;
16584 listitem_T *item, *item2;
16585 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016586 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016587 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016588 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016589 dict_T *d;
16590 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016591 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016592
Bram Moolenaar8c711452005-01-14 21:53:12 +000016593 if (argvars[0].v_type == VAR_DICT)
16594 {
16595 if (argvars[2].v_type != VAR_UNKNOWN)
16596 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016597 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016598 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016600 key = get_tv_string_chk(&argvars[1]);
16601 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016603 di = dict_find(d, key, -1);
16604 if (di == NULL)
16605 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016606 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16607 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016608 {
16609 *rettv = di->di_tv;
16610 init_tv(&di->di_tv);
16611 dictitem_remove(d, di);
16612 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016613 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016614 }
16615 }
16616 else if (argvars[0].v_type != VAR_LIST)
16617 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016618 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016619 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016621 int error = FALSE;
16622
16623 idx = get_tv_number_chk(&argvars[1], &error);
16624 if (error)
16625 ; /* type error: do nothing, errmsg already given */
16626 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016627 EMSGN(_(e_listidx), idx);
16628 else
16629 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016630 if (argvars[2].v_type == VAR_UNKNOWN)
16631 {
16632 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016633 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016634 *rettv = item->li_tv;
16635 vim_free(item);
16636 }
16637 else
16638 {
16639 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016640 end = get_tv_number_chk(&argvars[2], &error);
16641 if (error)
16642 ; /* type error: do nothing */
16643 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016644 EMSGN(_(e_listidx), end);
16645 else
16646 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016647 int cnt = 0;
16648
16649 for (li = item; li != NULL; li = li->li_next)
16650 {
16651 ++cnt;
16652 if (li == item2)
16653 break;
16654 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016655 if (li == NULL) /* didn't find "item2" after "item" */
16656 EMSG(_(e_invrange));
16657 else
16658 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016659 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016660 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016661 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016662 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016663 l->lv_first = item;
16664 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016665 item->li_prev = NULL;
16666 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016667 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016668 }
16669 }
16670 }
16671 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016672 }
16673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674}
16675
16676/*
16677 * "rename({from}, {to})" function
16678 */
16679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016680f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681{
16682 char_u buf[NUMBUFLEN];
16683
16684 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016685 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016687 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16688 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689}
16690
16691/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016692 * "repeat()" function
16693 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016695f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016696{
16697 char_u *p;
16698 int n;
16699 int slen;
16700 int len;
16701 char_u *r;
16702 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016703
16704 n = get_tv_number(&argvars[1]);
16705 if (argvars[0].v_type == VAR_LIST)
16706 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016707 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016708 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016709 if (list_extend(rettv->vval.v_list,
16710 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016711 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016712 }
16713 else
16714 {
16715 p = get_tv_string(&argvars[0]);
16716 rettv->v_type = VAR_STRING;
16717 rettv->vval.v_string = NULL;
16718
16719 slen = (int)STRLEN(p);
16720 len = slen * n;
16721 if (len <= 0)
16722 return;
16723
16724 r = alloc(len + 1);
16725 if (r != NULL)
16726 {
16727 for (i = 0; i < n; i++)
16728 mch_memmove(r + i * slen, p, (size_t)slen);
16729 r[len] = NUL;
16730 }
16731
16732 rettv->vval.v_string = r;
16733 }
16734}
16735
16736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 * "resolve()" function
16738 */
16739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016740f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741{
16742 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016743#ifdef HAVE_READLINK
16744 char_u *buf = NULL;
16745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016747 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748#ifdef FEAT_SHORTCUT
16749 {
16750 char_u *v = NULL;
16751
16752 v = mch_resolve_shortcut(p);
16753 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016754 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016756 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757 }
16758#else
16759# ifdef HAVE_READLINK
16760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 char_u *cpy;
16762 int len;
16763 char_u *remain = NULL;
16764 char_u *q;
16765 int is_relative_to_current = FALSE;
16766 int has_trailing_pathsep = FALSE;
16767 int limit = 100;
16768
16769 p = vim_strsave(p);
16770
16771 if (p[0] == '.' && (vim_ispathsep(p[1])
16772 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16773 is_relative_to_current = TRUE;
16774
16775 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016776 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016779 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781
16782 q = getnextcomp(p);
16783 if (*q != NUL)
16784 {
16785 /* Separate the first path component in "p", and keep the
16786 * remainder (beginning with the path separator). */
16787 remain = vim_strsave(q - 1);
16788 q[-1] = NUL;
16789 }
16790
Bram Moolenaard9462e32011-04-11 21:35:11 +020016791 buf = alloc(MAXPATHL + 1);
16792 if (buf == NULL)
16793 goto fail;
16794
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 for (;;)
16796 {
16797 for (;;)
16798 {
16799 len = readlink((char *)p, (char *)buf, MAXPATHL);
16800 if (len <= 0)
16801 break;
16802 buf[len] = NUL;
16803
16804 if (limit-- == 0)
16805 {
16806 vim_free(p);
16807 vim_free(remain);
16808 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016809 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 goto fail;
16811 }
16812
16813 /* Ensure that the result will have a trailing path separator
16814 * if the argument has one. */
16815 if (remain == NULL && has_trailing_pathsep)
16816 add_pathsep(buf);
16817
16818 /* Separate the first path component in the link value and
16819 * concatenate the remainders. */
16820 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16821 if (*q != NUL)
16822 {
16823 if (remain == NULL)
16824 remain = vim_strsave(q - 1);
16825 else
16826 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016827 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 if (cpy != NULL)
16829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 vim_free(remain);
16831 remain = cpy;
16832 }
16833 }
16834 q[-1] = NUL;
16835 }
16836
16837 q = gettail(p);
16838 if (q > p && *q == NUL)
16839 {
16840 /* Ignore trailing path separator. */
16841 q[-1] = NUL;
16842 q = gettail(p);
16843 }
16844 if (q > p && !mch_isFullName(buf))
16845 {
16846 /* symlink is relative to directory of argument */
16847 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16848 if (cpy != NULL)
16849 {
16850 STRCPY(cpy, p);
16851 STRCPY(gettail(cpy), buf);
16852 vim_free(p);
16853 p = cpy;
16854 }
16855 }
16856 else
16857 {
16858 vim_free(p);
16859 p = vim_strsave(buf);
16860 }
16861 }
16862
16863 if (remain == NULL)
16864 break;
16865
16866 /* Append the first path component of "remain" to "p". */
16867 q = getnextcomp(remain + 1);
16868 len = q - remain - (*q != NUL);
16869 cpy = vim_strnsave(p, STRLEN(p) + len);
16870 if (cpy != NULL)
16871 {
16872 STRNCAT(cpy, remain, len);
16873 vim_free(p);
16874 p = cpy;
16875 }
16876 /* Shorten "remain". */
16877 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016878 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 else
16880 {
16881 vim_free(remain);
16882 remain = NULL;
16883 }
16884 }
16885
16886 /* If the result is a relative path name, make it explicitly relative to
16887 * the current directory if and only if the argument had this form. */
16888 if (!vim_ispathsep(*p))
16889 {
16890 if (is_relative_to_current
16891 && *p != NUL
16892 && !(p[0] == '.'
16893 && (p[1] == NUL
16894 || vim_ispathsep(p[1])
16895 || (p[1] == '.'
16896 && (p[2] == NUL
16897 || vim_ispathsep(p[2]))))))
16898 {
16899 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016900 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 if (cpy != NULL)
16902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 vim_free(p);
16904 p = cpy;
16905 }
16906 }
16907 else if (!is_relative_to_current)
16908 {
16909 /* Strip leading "./". */
16910 q = p;
16911 while (q[0] == '.' && vim_ispathsep(q[1]))
16912 q += 2;
16913 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016914 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 }
16916 }
16917
16918 /* Ensure that the result will have no trailing path separator
16919 * if the argument had none. But keep "/" or "//". */
16920 if (!has_trailing_pathsep)
16921 {
16922 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016923 if (after_pathsep(p, q))
16924 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 }
16926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016927 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 }
16929# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016930 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931# endif
16932#endif
16933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016934 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935
16936#ifdef HAVE_READLINK
16937fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016938 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016940 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941}
16942
16943/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945 */
16946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016947f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948{
Bram Moolenaar33570922005-01-25 22:26:29 +000016949 list_T *l;
16950 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 if (argvars[0].v_type != VAR_LIST)
16953 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016954 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016955 && !tv_check_lock(l->lv_lock,
16956 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957 {
16958 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016959 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016960 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016961 while (li != NULL)
16962 {
16963 ni = li->li_prev;
16964 list_append(l, li);
16965 li = ni;
16966 }
16967 rettv->vval.v_list = l;
16968 rettv->v_type = VAR_LIST;
16969 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016970 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972}
16973
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016974#define SP_NOMOVE 0x01 /* don't move cursor */
16975#define SP_REPEAT 0x02 /* repeat to find outer pair */
16976#define SP_RETCOUNT 0x04 /* return matchcount */
16977#define SP_SETPCMARK 0x08 /* set previous context mark */
16978#define SP_START 0x10 /* accept match at start position */
16979#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16980#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016981#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016982
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016983static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984
16985/*
16986 * Get flags for a search function.
16987 * Possibly sets "p_ws".
16988 * Returns BACKWARD, FORWARD or zero (for an error).
16989 */
16990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016991get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992{
16993 int dir = FORWARD;
16994 char_u *flags;
16995 char_u nbuf[NUMBUFLEN];
16996 int mask;
16997
16998 if (varp->v_type != VAR_UNKNOWN)
16999 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017000 flags = get_tv_string_buf_chk(varp, nbuf);
17001 if (flags == NULL)
17002 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017003 while (*flags != NUL)
17004 {
17005 switch (*flags)
17006 {
17007 case 'b': dir = BACKWARD; break;
17008 case 'w': p_ws = TRUE; break;
17009 case 'W': p_ws = FALSE; break;
17010 default: mask = 0;
17011 if (flagsp != NULL)
17012 switch (*flags)
17013 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017014 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017015 case 'e': mask = SP_END; break;
17016 case 'm': mask = SP_RETCOUNT; break;
17017 case 'n': mask = SP_NOMOVE; break;
17018 case 'p': mask = SP_SUBPAT; break;
17019 case 'r': mask = SP_REPEAT; break;
17020 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017021 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017022 }
17023 if (mask == 0)
17024 {
17025 EMSG2(_(e_invarg2), flags);
17026 dir = 0;
17027 }
17028 else
17029 *flagsp |= mask;
17030 }
17031 if (dir == 0)
17032 break;
17033 ++flags;
17034 }
17035 }
17036 return dir;
17037}
17038
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017040 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017042 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017043search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017045 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046 char_u *pat;
17047 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017048 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049 int save_p_ws = p_ws;
17050 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017051 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017052 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017053 proftime_T tm;
17054#ifdef FEAT_RELTIME
17055 long time_limit = 0;
17056#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017057 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017058 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017060 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017061 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017062 if (dir == 0)
17063 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017064 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017065 if (flags & SP_START)
17066 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017067 if (flags & SP_END)
17068 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017069 if (flags & SP_COLUMN)
17070 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017071
Bram Moolenaar76929292008-01-06 19:07:36 +000017072 /* Optional arguments: line number to stop searching and timeout. */
17073 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017074 {
17075 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17076 if (lnum_stop < 0)
17077 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017078#ifdef FEAT_RELTIME
17079 if (argvars[3].v_type != VAR_UNKNOWN)
17080 {
17081 time_limit = get_tv_number_chk(&argvars[3], NULL);
17082 if (time_limit < 0)
17083 goto theend;
17084 }
17085#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017086 }
17087
Bram Moolenaar76929292008-01-06 19:07:36 +000017088#ifdef FEAT_RELTIME
17089 /* Set the time limit, if there is one. */
17090 profile_setlimit(time_limit, &tm);
17091#endif
17092
Bram Moolenaar231334e2005-07-25 20:46:57 +000017093 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017094 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017095 * Check to make sure only those flags are set.
17096 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17097 * flags cannot be set. Check for that condition also.
17098 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017099 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017100 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017102 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017103 goto theend;
17104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017106 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017107 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017108 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017109 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017111 if (flags & SP_SUBPAT)
17112 retval = subpatnum;
17113 else
17114 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017115 if (flags & SP_SETPCMARK)
17116 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017118 if (match_pos != NULL)
17119 {
17120 /* Store the match cursor position */
17121 match_pos->lnum = pos.lnum;
17122 match_pos->col = pos.col + 1;
17123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 /* "/$" will put the cursor after the end of the line, may need to
17125 * correct that here */
17126 check_cursor();
17127 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017128
17129 /* If 'n' flag is used: restore cursor position. */
17130 if (flags & SP_NOMOVE)
17131 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017132 else
17133 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017134theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017136
17137 return retval;
17138}
17139
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017140#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017141
17142/*
17143 * round() is not in C90, use ceil() or floor() instead.
17144 */
17145 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017146vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017147{
17148 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17149}
17150
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017151/*
17152 * "round({float})" function
17153 */
17154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017155f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017156{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017157 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017158
17159 rettv->v_type = VAR_FLOAT;
17160 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017161 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017162 else
17163 rettv->vval.v_float = 0.0;
17164}
17165#endif
17166
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017167/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017168 * "screenattr()" function
17169 */
17170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017171f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017172{
17173 int row;
17174 int col;
17175 int c;
17176
17177 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17178 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17179 if (row < 0 || row >= screen_Rows
17180 || col < 0 || col >= screen_Columns)
17181 c = -1;
17182 else
17183 c = ScreenAttrs[LineOffset[row] + col];
17184 rettv->vval.v_number = c;
17185}
17186
17187/*
17188 * "screenchar()" function
17189 */
17190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017191f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017192{
17193 int row;
17194 int col;
17195 int off;
17196 int c;
17197
17198 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17199 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17200 if (row < 0 || row >= screen_Rows
17201 || col < 0 || col >= screen_Columns)
17202 c = -1;
17203 else
17204 {
17205 off = LineOffset[row] + col;
17206#ifdef FEAT_MBYTE
17207 if (enc_utf8 && ScreenLinesUC[off] != 0)
17208 c = ScreenLinesUC[off];
17209 else
17210#endif
17211 c = ScreenLines[off];
17212 }
17213 rettv->vval.v_number = c;
17214}
17215
17216/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017217 * "screencol()" function
17218 *
17219 * First column is 1 to be consistent with virtcol().
17220 */
17221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017222f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017223{
17224 rettv->vval.v_number = screen_screencol() + 1;
17225}
17226
17227/*
17228 * "screenrow()" function
17229 */
17230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017231f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017232{
17233 rettv->vval.v_number = screen_screenrow() + 1;
17234}
17235
17236/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017237 * "search()" function
17238 */
17239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017240f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017241{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017242 int flags = 0;
17243
17244 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245}
17246
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017248 * "searchdecl()" function
17249 */
17250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017251f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017252{
17253 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017254 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017255 int error = FALSE;
17256 char_u *name;
17257
17258 rettv->vval.v_number = 1; /* default: FAIL */
17259
17260 name = get_tv_string_chk(&argvars[0]);
17261 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017262 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017263 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017264 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17265 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17266 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017267 if (!error && name != NULL)
17268 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017269 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017270}
17271
17272/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017273 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017275 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017276searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277{
17278 char_u *spat, *mpat, *epat;
17279 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 int dir;
17282 int flags = 0;
17283 char_u nbuf1[NUMBUFLEN];
17284 char_u nbuf2[NUMBUFLEN];
17285 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017286 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017287 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017288 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017291 spat = get_tv_string_chk(&argvars[0]);
17292 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17293 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17294 if (spat == NULL || mpat == NULL || epat == NULL)
17295 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 /* Handle the optional fourth argument: flags */
17298 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017299 if (dir == 0)
17300 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017301
17302 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017303 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17304 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017305 if ((flags & (SP_END | SP_SUBPAT)) != 0
17306 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017307 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017308 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017309 goto theend;
17310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017312 /* Using 'r' implies 'W', otherwise it doesn't work. */
17313 if (flags & SP_REPEAT)
17314 p_ws = FALSE;
17315
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017316 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017317 if (argvars[3].v_type == VAR_UNKNOWN
17318 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 skip = (char_u *)"";
17320 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017322 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017323 if (argvars[5].v_type != VAR_UNKNOWN)
17324 {
17325 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17326 if (lnum_stop < 0)
17327 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017328#ifdef FEAT_RELTIME
17329 if (argvars[6].v_type != VAR_UNKNOWN)
17330 {
17331 time_limit = get_tv_number_chk(&argvars[6], NULL);
17332 if (time_limit < 0)
17333 goto theend;
17334 }
17335#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017336 }
17337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017338 if (skip == NULL)
17339 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017341 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017342 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017343
17344theend:
17345 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017346
17347 return retval;
17348}
17349
17350/*
17351 * "searchpair()" function
17352 */
17353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017354f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017355{
17356 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17357}
17358
17359/*
17360 * "searchpairpos()" function
17361 */
17362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017363f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017364{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017365 pos_T match_pos;
17366 int lnum = 0;
17367 int col = 0;
17368
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017369 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017370 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017371
17372 if (searchpair_cmn(argvars, &match_pos) > 0)
17373 {
17374 lnum = match_pos.lnum;
17375 col = match_pos.col;
17376 }
17377
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017378 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17379 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017380}
17381
17382/*
17383 * Search for a start/middle/end thing.
17384 * Used by searchpair(), see its documentation for the details.
17385 * Returns 0 or -1 for no match,
17386 */
17387 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017388do_searchpair(
17389 char_u *spat, /* start pattern */
17390 char_u *mpat, /* middle pattern */
17391 char_u *epat, /* end pattern */
17392 int dir, /* BACKWARD or FORWARD */
17393 char_u *skip, /* skip expression */
17394 int flags, /* SP_SETPCMARK and other SP_ values */
17395 pos_T *match_pos,
17396 linenr_T lnum_stop, /* stop at this line if not zero */
17397 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017398{
17399 char_u *save_cpo;
17400 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17401 long retval = 0;
17402 pos_T pos;
17403 pos_T firstpos;
17404 pos_T foundpos;
17405 pos_T save_cursor;
17406 pos_T save_pos;
17407 int n;
17408 int r;
17409 int nest = 1;
17410 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017411 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017412 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017413
17414 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17415 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017416 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017417
Bram Moolenaar76929292008-01-06 19:07:36 +000017418#ifdef FEAT_RELTIME
17419 /* Set the time limit, if there is one. */
17420 profile_setlimit(time_limit, &tm);
17421#endif
17422
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017423 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17424 * start/middle/end (pat3, for the top pair). */
17425 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17426 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17427 if (pat2 == NULL || pat3 == NULL)
17428 goto theend;
17429 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17430 if (*mpat == NUL)
17431 STRCPY(pat3, pat2);
17432 else
17433 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17434 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017435 if (flags & SP_START)
17436 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017437
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438 save_cursor = curwin->w_cursor;
17439 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017440 clearpos(&firstpos);
17441 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 pat = pat3;
17443 for (;;)
17444 {
17445 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017446 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17448 /* didn't find it or found the first match again: FAIL */
17449 break;
17450
17451 if (firstpos.lnum == 0)
17452 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017453 if (equalpos(pos, foundpos))
17454 {
17455 /* Found the same position again. Can happen with a pattern that
17456 * has "\zs" at the end and searching backwards. Advance one
17457 * character and try again. */
17458 if (dir == BACKWARD)
17459 decl(&pos);
17460 else
17461 incl(&pos);
17462 }
17463 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017465 /* clear the start flag to avoid getting stuck here */
17466 options &= ~SEARCH_START;
17467
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 /* If the skip pattern matches, ignore this match. */
17469 if (*skip != NUL)
17470 {
17471 save_pos = curwin->w_cursor;
17472 curwin->w_cursor = pos;
17473 r = eval_to_bool(skip, &err, NULL, FALSE);
17474 curwin->w_cursor = save_pos;
17475 if (err)
17476 {
17477 /* Evaluating {skip} caused an error, break here. */
17478 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017479 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 break;
17481 }
17482 if (r)
17483 continue;
17484 }
17485
17486 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17487 {
17488 /* Found end when searching backwards or start when searching
17489 * forward: nested pair. */
17490 ++nest;
17491 pat = pat2; /* nested, don't search for middle */
17492 }
17493 else
17494 {
17495 /* Found end when searching forward or start when searching
17496 * backward: end of (nested) pair; or found middle in outer pair. */
17497 if (--nest == 1)
17498 pat = pat3; /* outer level, search for middle */
17499 }
17500
17501 if (nest == 0)
17502 {
17503 /* Found the match: return matchcount or line number. */
17504 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017505 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017507 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017508 if (flags & SP_SETPCMARK)
17509 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 curwin->w_cursor = pos;
17511 if (!(flags & SP_REPEAT))
17512 break;
17513 nest = 1; /* search for next unmatched */
17514 }
17515 }
17516
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017517 if (match_pos != NULL)
17518 {
17519 /* Store the match cursor position */
17520 match_pos->lnum = curwin->w_cursor.lnum;
17521 match_pos->col = curwin->w_cursor.col + 1;
17522 }
17523
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017525 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 curwin->w_cursor = save_cursor;
17527
17528theend:
17529 vim_free(pat2);
17530 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017531 if (p_cpo == empty_option)
17532 p_cpo = save_cpo;
17533 else
17534 /* Darn, evaluating the {skip} expression changed the value. */
17535 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017536
17537 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538}
17539
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017540/*
17541 * "searchpos()" function
17542 */
17543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017544f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017545{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017546 pos_T match_pos;
17547 int lnum = 0;
17548 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017549 int n;
17550 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017551
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017552 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017553 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017554
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017555 n = search_cmn(argvars, &match_pos, &flags);
17556 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017557 {
17558 lnum = match_pos.lnum;
17559 col = match_pos.col;
17560 }
17561
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017562 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17563 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017564 if (flags & SP_SUBPAT)
17565 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017566}
17567
Bram Moolenaar0d660222005-01-07 21:51:51 +000017568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017569f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017571#ifdef FEAT_CLIENTSERVER
17572 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017573 char_u *server = get_tv_string_chk(&argvars[0]);
17574 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575
Bram Moolenaar0d660222005-01-07 21:51:51 +000017576 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017577 if (server == NULL || reply == NULL)
17578 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579 if (check_restricted() || check_secure())
17580 return;
17581# ifdef FEAT_X11
17582 if (check_connection() == FAIL)
17583 return;
17584# endif
17585
17586 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017588 EMSG(_("E258: Unable to send to client"));
17589 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017591 rettv->vval.v_number = 0;
17592#else
17593 rettv->vval.v_number = -1;
17594#endif
17595}
17596
Bram Moolenaar0d660222005-01-07 21:51:51 +000017597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017598f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017599{
17600 char_u *r = NULL;
17601
17602#ifdef FEAT_CLIENTSERVER
17603# ifdef WIN32
17604 r = serverGetVimNames();
17605# else
17606 make_connection();
17607 if (X_DISPLAY != NULL)
17608 r = serverGetVimNames(X_DISPLAY);
17609# endif
17610#endif
17611 rettv->v_type = VAR_STRING;
17612 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613}
17614
17615/*
17616 * "setbufvar()" function
17617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017619f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620{
17621 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017624 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 char_u nbuf[NUMBUFLEN];
17626
17627 if (check_restricted() || check_secure())
17628 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017629 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17630 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017631 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 varp = &argvars[2];
17633
17634 if (buf != NULL && varname != NULL && varp != NULL)
17635 {
17636 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638
17639 if (*varname == '&')
17640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017641 long numval;
17642 char_u *strval;
17643 int error = FALSE;
17644
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017646 numval = get_tv_number_chk(varp, &error);
17647 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017648 if (!error && strval != NULL)
17649 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 }
17651 else
17652 {
17653 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17654 if (bufvarname != NULL)
17655 {
17656 STRCPY(bufvarname, "b:");
17657 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017658 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 vim_free(bufvarname);
17660 }
17661 }
17662
17663 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666}
17667
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017669f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017670{
17671 dict_T *d;
17672 dictitem_T *di;
17673 char_u *csearch;
17674
17675 if (argvars[0].v_type != VAR_DICT)
17676 {
17677 EMSG(_(e_dictreq));
17678 return;
17679 }
17680
17681 if ((d = argvars[0].vval.v_dict) != NULL)
17682 {
17683 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17684 if (csearch != NULL)
17685 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017686#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017687 if (enc_utf8)
17688 {
17689 int pcc[MAX_MCO];
17690 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017691
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017692 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17693 }
17694 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017695#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017696 set_last_csearch(PTR2CHAR(csearch),
17697 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017698 }
17699
17700 di = dict_find(d, (char_u *)"forward", -1);
17701 if (di != NULL)
17702 set_csearch_direction(get_tv_number(&di->di_tv)
17703 ? FORWARD : BACKWARD);
17704
17705 di = dict_find(d, (char_u *)"until", -1);
17706 if (di != NULL)
17707 set_csearch_until(!!get_tv_number(&di->di_tv));
17708 }
17709}
17710
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711/*
17712 * "setcmdpos()" function
17713 */
17714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017715f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017717 int pos = (int)get_tv_number(&argvars[0]) - 1;
17718
17719 if (pos >= 0)
17720 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721}
17722
17723/*
17724 * "setline()" function
17725 */
17726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017727f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728{
17729 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017730 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017731 list_T *l = NULL;
17732 listitem_T *li = NULL;
17733 long added = 0;
17734 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017736 lnum = get_tv_lnum(&argvars[0]);
17737 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017739 l = argvars[1].vval.v_list;
17740 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017742 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017743 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017744
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017745 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017746 for (;;)
17747 {
17748 if (l != NULL)
17749 {
17750 /* list argument, get next string */
17751 if (li == NULL)
17752 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017753 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017754 li = li->li_next;
17755 }
17756
17757 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017758 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017759 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017760
17761 /* When coming here from Insert mode, sync undo, so that this can be
17762 * undone separately from what was previously inserted. */
17763 if (u_sync_once == 2)
17764 {
17765 u_sync_once = 1; /* notify that u_sync() was called */
17766 u_sync(TRUE);
17767 }
17768
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017769 if (lnum <= curbuf->b_ml.ml_line_count)
17770 {
17771 /* existing line, replace it */
17772 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17773 {
17774 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017775 if (lnum == curwin->w_cursor.lnum)
17776 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017777 rettv->vval.v_number = 0; /* OK */
17778 }
17779 }
17780 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17781 {
17782 /* lnum is one past the last line, append the line */
17783 ++added;
17784 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17785 rettv->vval.v_number = 0; /* OK */
17786 }
17787
17788 if (l == NULL) /* only one string argument */
17789 break;
17790 ++lnum;
17791 }
17792
17793 if (added > 0)
17794 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795}
17796
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017797static 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 +000017798
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017800 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017801 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017803set_qf_ll_list(
17804 win_T *wp UNUSED,
17805 typval_T *list_arg UNUSED,
17806 typval_T *action_arg UNUSED,
17807 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017808{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017809#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017810 char_u *act;
17811 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017812#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017813
Bram Moolenaar2641f772005-03-25 21:58:17 +000017814 rettv->vval.v_number = -1;
17815
17816#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017817 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017818 EMSG(_(e_listreq));
17819 else
17820 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017821 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017822
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017823 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017824 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017825 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017826 if (act == NULL)
17827 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017828 if (*act == 'a' || *act == 'r')
17829 action = *act;
17830 }
17831
Bram Moolenaar81484f42012-12-05 15:16:47 +010017832 if (l != NULL && set_errorlist(wp, l, action,
17833 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017834 rettv->vval.v_number = 0;
17835 }
17836#endif
17837}
17838
17839/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017840 * "setloclist()" function
17841 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017843f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017844{
17845 win_T *win;
17846
17847 rettv->vval.v_number = -1;
17848
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017849 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017850 if (win != NULL)
17851 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17852}
17853
17854/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017855 * "setmatches()" function
17856 */
17857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017858f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017859{
17860#ifdef FEAT_SEARCH_EXTRA
17861 list_T *l;
17862 listitem_T *li;
17863 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017864 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017865
17866 rettv->vval.v_number = -1;
17867 if (argvars[0].v_type != VAR_LIST)
17868 {
17869 EMSG(_(e_listreq));
17870 return;
17871 }
17872 if ((l = argvars[0].vval.v_list) != NULL)
17873 {
17874
17875 /* To some extent make sure that we are dealing with a list from
17876 * "getmatches()". */
17877 li = l->lv_first;
17878 while (li != NULL)
17879 {
17880 if (li->li_tv.v_type != VAR_DICT
17881 || (d = li->li_tv.vval.v_dict) == NULL)
17882 {
17883 EMSG(_(e_invarg));
17884 return;
17885 }
17886 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017887 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17888 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017889 && dict_find(d, (char_u *)"priority", -1) != NULL
17890 && dict_find(d, (char_u *)"id", -1) != NULL))
17891 {
17892 EMSG(_(e_invarg));
17893 return;
17894 }
17895 li = li->li_next;
17896 }
17897
17898 clear_matches(curwin);
17899 li = l->lv_first;
17900 while (li != NULL)
17901 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017902 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017903 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017904 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017905 char_u *group;
17906 int priority;
17907 int id;
17908 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017909
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017910 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017911 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17912 {
17913 if (s == NULL)
17914 {
17915 s = list_alloc();
17916 if (s == NULL)
17917 return;
17918 }
17919
17920 /* match from matchaddpos() */
17921 for (i = 1; i < 9; i++)
17922 {
17923 sprintf((char *)buf, (char *)"pos%d", i);
17924 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17925 {
17926 if (di->di_tv.v_type != VAR_LIST)
17927 return;
17928
17929 list_append_tv(s, &di->di_tv);
17930 s->lv_refcount++;
17931 }
17932 else
17933 break;
17934 }
17935 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017936
17937 group = get_dict_string(d, (char_u *)"group", FALSE);
17938 priority = (int)get_dict_number(d, (char_u *)"priority");
17939 id = (int)get_dict_number(d, (char_u *)"id");
17940 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17941 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17942 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017943 if (i == 0)
17944 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017945 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017946 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017947 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017948 }
17949 else
17950 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017951 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017952 list_unref(s);
17953 s = NULL;
17954 }
17955
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017956 li = li->li_next;
17957 }
17958 rettv->vval.v_number = 0;
17959 }
17960#endif
17961}
17962
17963/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017964 * "setpos()" function
17965 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017967f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017968{
17969 pos_T pos;
17970 int fnum;
17971 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017972 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017973
Bram Moolenaar08250432008-02-13 11:42:46 +000017974 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017975 name = get_tv_string_chk(argvars);
17976 if (name != NULL)
17977 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017978 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017979 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017980 if (--pos.col < 0)
17981 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017982 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017983 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017984 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017985 if (fnum == curbuf->b_fnum)
17986 {
17987 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017988 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017989 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017990 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017991 curwin->w_set_curswant = FALSE;
17992 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017993 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017994 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017995 }
17996 else
17997 EMSG(_(e_invarg));
17998 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017999 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18000 {
18001 /* set mark */
18002 if (setmark_pos(name[1], &pos, fnum) == OK)
18003 rettv->vval.v_number = 0;
18004 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018005 else
18006 EMSG(_(e_invarg));
18007 }
18008 }
18009}
18010
18011/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018012 * "setqflist()" function
18013 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018015f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018016{
18017 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18018}
18019
18020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 * "setreg()" function
18022 */
18023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018024f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025{
18026 int regname;
18027 char_u *strregname;
18028 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018029 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 int append;
18031 char_u yank_type;
18032 long block_len;
18033
18034 block_len = -1;
18035 yank_type = MAUTO;
18036 append = FALSE;
18037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018038 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018039 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018041 if (strregname == NULL)
18042 return; /* type error; errmsg already given */
18043 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 if (regname == 0 || regname == '@')
18045 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018047 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018049 stropt = get_tv_string_chk(&argvars[2]);
18050 if (stropt == NULL)
18051 return; /* type error */
18052 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 switch (*stropt)
18054 {
18055 case 'a': case 'A': /* append */
18056 append = TRUE;
18057 break;
18058 case 'v': case 'c': /* character-wise selection */
18059 yank_type = MCHAR;
18060 break;
18061 case 'V': case 'l': /* line-wise selection */
18062 yank_type = MLINE;
18063 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 case 'b': case Ctrl_V: /* block-wise selection */
18065 yank_type = MBLOCK;
18066 if (VIM_ISDIGIT(stropt[1]))
18067 {
18068 ++stropt;
18069 block_len = getdigits(&stropt) - 1;
18070 --stropt;
18071 }
18072 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 }
18074 }
18075
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018076 if (argvars[1].v_type == VAR_LIST)
18077 {
18078 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018079 char_u **allocval;
18080 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018081 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018082 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018083 int len = argvars[1].vval.v_list->lv_len;
18084 listitem_T *li;
18085
Bram Moolenaar7d647822014-04-05 21:28:56 +020018086 /* First half: use for pointers to result lines; second half: use for
18087 * pointers to allocated copies. */
18088 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018089 if (lstval == NULL)
18090 return;
18091 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018092 allocval = lstval + len + 2;
18093 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018094
18095 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18096 li = li->li_next)
18097 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018098 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018099 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018100 goto free_lstval;
18101 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018102 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018103 /* Need to make a copy, next get_tv_string_buf_chk() will
18104 * overwrite the string. */
18105 strval = vim_strsave(buf);
18106 if (strval == NULL)
18107 goto free_lstval;
18108 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018109 }
18110 *curval++ = strval;
18111 }
18112 *curval++ = NULL;
18113
18114 write_reg_contents_lst(regname, lstval, -1,
18115 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018116free_lstval:
18117 while (curallocval > allocval)
18118 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018119 vim_free(lstval);
18120 }
18121 else
18122 {
18123 strval = get_tv_string_chk(&argvars[1]);
18124 if (strval == NULL)
18125 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018126 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018128 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018129 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130}
18131
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018132/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018133 * "settabvar()" function
18134 */
18135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018136f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018137{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018138#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018139 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018140 tabpage_T *tp;
18141#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018142 char_u *varname, *tabvarname;
18143 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018144
18145 rettv->vval.v_number = 0;
18146
18147 if (check_restricted() || check_secure())
18148 return;
18149
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018150#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018151 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018152#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018153 varname = get_tv_string_chk(&argvars[1]);
18154 varp = &argvars[2];
18155
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018156 if (varname != NULL && varp != NULL
18157#ifdef FEAT_WINDOWS
18158 && tp != NULL
18159#endif
18160 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018161 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018162#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018163 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018164 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018165#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018166
18167 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18168 if (tabvarname != NULL)
18169 {
18170 STRCPY(tabvarname, "t:");
18171 STRCPY(tabvarname + 2, varname);
18172 set_var(tabvarname, varp, TRUE);
18173 vim_free(tabvarname);
18174 }
18175
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018176#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018177 /* Restore current tabpage */
18178 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018179 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018180#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018181 }
18182}
18183
18184/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018185 * "settabwinvar()" function
18186 */
18187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018188f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018189{
18190 setwinvar(argvars, rettv, 1);
18191}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192
18193/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018194 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018197f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018199 setwinvar(argvars, rettv, 0);
18200}
18201
18202/*
18203 * "setwinvar()" and "settabwinvar()" functions
18204 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018205
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018207setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018208{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 win_T *win;
18210#ifdef FEAT_WINDOWS
18211 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018212 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018213 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214#endif
18215 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018216 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018218 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219
18220 if (check_restricted() || check_secure())
18221 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018222
18223#ifdef FEAT_WINDOWS
18224 if (off == 1)
18225 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18226 else
18227 tp = curtab;
18228#endif
18229 win = find_win_by_nr(&argvars[off], tp);
18230 varname = get_tv_string_chk(&argvars[off + 1]);
18231 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232
18233 if (win != NULL && varname != NULL && varp != NULL)
18234 {
18235#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018236 need_switch_win = !(tp == curtab && win == curwin);
18237 if (!need_switch_win
18238 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018241 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018243 long numval;
18244 char_u *strval;
18245 int error = FALSE;
18246
18247 ++varname;
18248 numval = get_tv_number_chk(varp, &error);
18249 strval = get_tv_string_buf_chk(varp, nbuf);
18250 if (!error && strval != NULL)
18251 set_option_value(varname, numval, strval, OPT_LOCAL);
18252 }
18253 else
18254 {
18255 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18256 if (winvarname != NULL)
18257 {
18258 STRCPY(winvarname, "w:");
18259 STRCPY(winvarname + 2, varname);
18260 set_var(winvarname, varp, TRUE);
18261 vim_free(winvarname);
18262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 }
18264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018266 if (need_switch_win)
18267 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268#endif
18269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270}
18271
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018272#ifdef FEAT_CRYPT
18273/*
18274 * "sha256({string})" function
18275 */
18276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018277f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018278{
18279 char_u *p;
18280
18281 p = get_tv_string(&argvars[0]);
18282 rettv->vval.v_string = vim_strsave(
18283 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18284 rettv->v_type = VAR_STRING;
18285}
18286#endif /* FEAT_CRYPT */
18287
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018289 * "shellescape({string})" function
18290 */
18291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018292f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018293{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018294 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018295 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018296 rettv->v_type = VAR_STRING;
18297}
18298
18299/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018300 * shiftwidth() function
18301 */
18302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018303f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018304{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018305 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018306}
18307
18308/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018309 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 */
18311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018312f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018314 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315
Bram Moolenaar0d660222005-01-07 21:51:51 +000018316 p = get_tv_string(&argvars[0]);
18317 rettv->vval.v_string = vim_strsave(p);
18318 simplify_filename(rettv->vval.v_string); /* simplify in place */
18319 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320}
18321
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018322#ifdef FEAT_FLOAT
18323/*
18324 * "sin()" function
18325 */
18326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018327f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018328{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018329 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018330
18331 rettv->v_type = VAR_FLOAT;
18332 if (get_float_arg(argvars, &f) == OK)
18333 rettv->vval.v_float = sin(f);
18334 else
18335 rettv->vval.v_float = 0.0;
18336}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018337
18338/*
18339 * "sinh()" function
18340 */
18341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018342f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018343{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018344 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018345
18346 rettv->v_type = VAR_FLOAT;
18347 if (get_float_arg(argvars, &f) == OK)
18348 rettv->vval.v_float = sinh(f);
18349 else
18350 rettv->vval.v_float = 0.0;
18351}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018352#endif
18353
Bram Moolenaar0d660222005-01-07 21:51:51 +000018354static int
18355#ifdef __BORLANDC__
18356 _RTLENTRYF
18357#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018358 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018359static int
18360#ifdef __BORLANDC__
18361 _RTLENTRYF
18362#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018363 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018364
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018365/* struct used in the array that's given to qsort() */
18366typedef struct
18367{
18368 listitem_T *item;
18369 int idx;
18370} sortItem_T;
18371
Bram Moolenaar0d660222005-01-07 21:51:51 +000018372static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018373static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018374static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018375#ifdef FEAT_FLOAT
18376static int item_compare_float;
18377#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018378static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018379static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018380static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018381static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018382static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018383#define ITEM_COMPARE_FAIL 999
18384
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018386 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018388 static int
18389#ifdef __BORLANDC__
18390_RTLENTRYF
18391#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018392item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018394 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018395 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018396 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018397 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018398 int res;
18399 char_u numbuf1[NUMBUFLEN];
18400 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018402 si1 = (sortItem_T *)s1;
18403 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018404 tv1 = &si1->item->li_tv;
18405 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018406
18407 if (item_compare_numbers)
18408 {
18409 long v1 = get_tv_number(tv1);
18410 long v2 = get_tv_number(tv2);
18411
18412 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18413 }
18414
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018415#ifdef FEAT_FLOAT
18416 if (item_compare_float)
18417 {
18418 float_T v1 = get_tv_float(tv1);
18419 float_T v2 = get_tv_float(tv2);
18420
18421 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18422 }
18423#endif
18424
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018425 /* tv2string() puts quotes around a string and allocates memory. Don't do
18426 * that for string variables. Use a single quote when comparing with a
18427 * non-string to do what the docs promise. */
18428 if (tv1->v_type == VAR_STRING)
18429 {
18430 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18431 p1 = (char_u *)"'";
18432 else
18433 p1 = tv1->vval.v_string;
18434 }
18435 else
18436 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18437 if (tv2->v_type == VAR_STRING)
18438 {
18439 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18440 p2 = (char_u *)"'";
18441 else
18442 p2 = tv2->vval.v_string;
18443 }
18444 else
18445 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018446 if (p1 == NULL)
18447 p1 = (char_u *)"";
18448 if (p2 == NULL)
18449 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018450 if (!item_compare_numeric)
18451 {
18452 if (item_compare_ic)
18453 res = STRICMP(p1, p2);
18454 else
18455 res = STRCMP(p1, p2);
18456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018458 {
18459 double n1, n2;
18460 n1 = strtod((char *)p1, (char **)&p1);
18461 n2 = strtod((char *)p2, (char **)&p2);
18462 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18463 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018464
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018465 /* When the result would be zero, compare the item indexes. Makes the
18466 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018467 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018468 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018469
Bram Moolenaar0d660222005-01-07 21:51:51 +000018470 vim_free(tofree1);
18471 vim_free(tofree2);
18472 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473}
18474
18475 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018476#ifdef __BORLANDC__
18477_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018479item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018480{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018481 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018482 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018483 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018484 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018485 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018487 /* shortcut after failure in previous call; compare all items equal */
18488 if (item_compare_func_err)
18489 return 0;
18490
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018491 si1 = (sortItem_T *)s1;
18492 si2 = (sortItem_T *)s2;
18493
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018494 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018495 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018496 copy_tv(&si1->item->li_tv, &argv[0]);
18497 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018498
18499 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018500 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018501 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18502 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018503 clear_tv(&argv[0]);
18504 clear_tv(&argv[1]);
18505
18506 if (res == FAIL)
18507 res = ITEM_COMPARE_FAIL;
18508 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018509 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18510 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018511 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018512 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018513
18514 /* When the result would be zero, compare the pointers themselves. Makes
18515 * the sort stable. */
18516 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018517 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018518
Bram Moolenaar0d660222005-01-07 21:51:51 +000018519 return res;
18520}
18521
18522/*
18523 * "sort({list})" function
18524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018526do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527{
Bram Moolenaar33570922005-01-25 22:26:29 +000018528 list_T *l;
18529 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018530 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018531 long len;
18532 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533
Bram Moolenaar0d660222005-01-07 21:51:51 +000018534 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018535 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 else
18537 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018538 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018539 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018540 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18541 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018542 return;
18543 rettv->vval.v_list = l;
18544 rettv->v_type = VAR_LIST;
18545 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546
Bram Moolenaar0d660222005-01-07 21:51:51 +000018547 len = list_len(l);
18548 if (len <= 1)
18549 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550
Bram Moolenaar0d660222005-01-07 21:51:51 +000018551 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018552 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018553 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018554#ifdef FEAT_FLOAT
18555 item_compare_float = FALSE;
18556#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018557 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018558 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018559 if (argvars[1].v_type != VAR_UNKNOWN)
18560 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018561 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018562 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018563 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018564 else
18565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018566 int error = FALSE;
18567
18568 i = get_tv_number_chk(&argvars[1], &error);
18569 if (error)
18570 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018571 if (i == 1)
18572 item_compare_ic = TRUE;
18573 else
18574 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018575 if (item_compare_func != NULL)
18576 {
18577 if (STRCMP(item_compare_func, "n") == 0)
18578 {
18579 item_compare_func = NULL;
18580 item_compare_numeric = TRUE;
18581 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018582 else if (STRCMP(item_compare_func, "N") == 0)
18583 {
18584 item_compare_func = NULL;
18585 item_compare_numbers = TRUE;
18586 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018587#ifdef FEAT_FLOAT
18588 else if (STRCMP(item_compare_func, "f") == 0)
18589 {
18590 item_compare_func = NULL;
18591 item_compare_float = TRUE;
18592 }
18593#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018594 else if (STRCMP(item_compare_func, "i") == 0)
18595 {
18596 item_compare_func = NULL;
18597 item_compare_ic = TRUE;
18598 }
18599 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018600 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018601
18602 if (argvars[2].v_type != VAR_UNKNOWN)
18603 {
18604 /* optional third argument: {dict} */
18605 if (argvars[2].v_type != VAR_DICT)
18606 {
18607 EMSG(_(e_dictreq));
18608 return;
18609 }
18610 item_compare_selfdict = argvars[2].vval.v_dict;
18611 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613
Bram Moolenaar0d660222005-01-07 21:51:51 +000018614 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018615 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018616 if (ptrs == NULL)
18617 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618
Bram Moolenaar327aa022014-03-25 18:24:23 +010018619 i = 0;
18620 if (sort)
18621 {
18622 /* sort(): ptrs will be the list to sort */
18623 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018624 {
18625 ptrs[i].item = li;
18626 ptrs[i].idx = i;
18627 ++i;
18628 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018629
18630 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018631 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018632 /* test the compare function */
18633 if (item_compare_func != NULL
18634 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018635 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018636 EMSG(_("E702: Sort compare function failed"));
18637 else
18638 {
18639 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018640 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018641 item_compare_func == NULL ? item_compare : item_compare2);
18642
18643 if (!item_compare_func_err)
18644 {
18645 /* Clear the List and append the items in sorted order. */
18646 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18647 l->lv_len = 0;
18648 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018649 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018650 }
18651 }
18652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018655 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018656
18657 /* f_uniq(): ptrs will be a stack of items to remove */
18658 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018659 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018660 item_compare_func_ptr = item_compare_func
18661 ? item_compare2 : item_compare;
18662
18663 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18664 li = li->li_next)
18665 {
18666 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18667 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018668 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018669 if (item_compare_func_err)
18670 {
18671 EMSG(_("E882: Uniq compare function failed"));
18672 break;
18673 }
18674 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018676 if (!item_compare_func_err)
18677 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018678 while (--i >= 0)
18679 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018680 li = ptrs[i].item->li_next;
18681 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018682 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018683 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018684 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018685 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018686 list_fix_watch(l, li);
18687 listitem_free(li);
18688 l->lv_len--;
18689 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018690 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018691 }
18692
18693 vim_free(ptrs);
18694 }
18695}
18696
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018697/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018698 * "sort({list})" function
18699 */
18700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018701f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018702{
18703 do_sort_uniq(argvars, rettv, TRUE);
18704}
18705
18706/*
18707 * "uniq({list})" function
18708 */
18709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018710f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018711{
18712 do_sort_uniq(argvars, rettv, FALSE);
18713}
18714
18715/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018716 * "soundfold({word})" function
18717 */
18718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018719f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018720{
18721 char_u *s;
18722
18723 rettv->v_type = VAR_STRING;
18724 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018725#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018726 rettv->vval.v_string = eval_soundfold(s);
18727#else
18728 rettv->vval.v_string = vim_strsave(s);
18729#endif
18730}
18731
18732/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018733 * "spellbadword()" function
18734 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018736f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018737{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018738 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018739 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018740 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018741
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018742 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018743 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018744
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018745#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018746 if (argvars[0].v_type == VAR_UNKNOWN)
18747 {
18748 /* Find the start and length of the badly spelled word. */
18749 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18750 if (len != 0)
18751 word = ml_get_cursor();
18752 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018753 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018754 {
18755 char_u *str = get_tv_string_chk(&argvars[0]);
18756 int capcol = -1;
18757
18758 if (str != NULL)
18759 {
18760 /* Check the argument for spelling. */
18761 while (*str != NUL)
18762 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018763 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018764 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018765 {
18766 word = str;
18767 break;
18768 }
18769 str += len;
18770 }
18771 }
18772 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018773#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018774
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018775 list_append_string(rettv->vval.v_list, word, len);
18776 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018777 attr == HLF_SPB ? "bad" :
18778 attr == HLF_SPR ? "rare" :
18779 attr == HLF_SPL ? "local" :
18780 attr == HLF_SPC ? "caps" :
18781 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018782}
18783
18784/*
18785 * "spellsuggest()" function
18786 */
18787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018788f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018789{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018790#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018791 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018792 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018793 int maxcount;
18794 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018795 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018796 listitem_T *li;
18797 int need_capital = FALSE;
18798#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018799
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018800 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018801 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018802
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018803#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018804 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018805 {
18806 str = get_tv_string(&argvars[0]);
18807 if (argvars[1].v_type != VAR_UNKNOWN)
18808 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018809 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018810 if (maxcount <= 0)
18811 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018812 if (argvars[2].v_type != VAR_UNKNOWN)
18813 {
18814 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18815 if (typeerr)
18816 return;
18817 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018818 }
18819 else
18820 maxcount = 25;
18821
Bram Moolenaar4770d092006-01-12 23:22:24 +000018822 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018823
18824 for (i = 0; i < ga.ga_len; ++i)
18825 {
18826 str = ((char_u **)ga.ga_data)[i];
18827
18828 li = listitem_alloc();
18829 if (li == NULL)
18830 vim_free(str);
18831 else
18832 {
18833 li->li_tv.v_type = VAR_STRING;
18834 li->li_tv.v_lock = 0;
18835 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018836 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018837 }
18838 }
18839 ga_clear(&ga);
18840 }
18841#endif
18842}
18843
Bram Moolenaar0d660222005-01-07 21:51:51 +000018844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018845f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018846{
18847 char_u *str;
18848 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018849 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018850 regmatch_T regmatch;
18851 char_u patbuf[NUMBUFLEN];
18852 char_u *save_cpo;
18853 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018854 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018855 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018856 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018857
18858 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18859 save_cpo = p_cpo;
18860 p_cpo = (char_u *)"";
18861
18862 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018863 if (argvars[1].v_type != VAR_UNKNOWN)
18864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018865 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18866 if (pat == NULL)
18867 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018868 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018869 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018870 }
18871 if (pat == NULL || *pat == NUL)
18872 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018873
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018874 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018876 if (typeerr)
18877 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878
Bram Moolenaar0d660222005-01-07 21:51:51 +000018879 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18880 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018882 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018883 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018884 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018885 if (*str == NUL)
18886 match = FALSE; /* empty item at the end */
18887 else
18888 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018889 if (match)
18890 end = regmatch.startp[0];
18891 else
18892 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018893 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18894 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018895 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018896 if (list_append_string(rettv->vval.v_list, str,
18897 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018898 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018899 }
18900 if (!match)
18901 break;
18902 /* Advance to just after the match. */
18903 if (regmatch.endp[0] > str)
18904 col = 0;
18905 else
18906 {
18907 /* Don't get stuck at the same match. */
18908#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018909 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018910#else
18911 col = 1;
18912#endif
18913 }
18914 str = regmatch.endp[0];
18915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916
Bram Moolenaar473de612013-06-08 18:19:48 +020018917 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919
Bram Moolenaar0d660222005-01-07 21:51:51 +000018920 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921}
18922
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018923#ifdef FEAT_FLOAT
18924/*
18925 * "sqrt()" function
18926 */
18927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018928f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018929{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018930 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018931
18932 rettv->v_type = VAR_FLOAT;
18933 if (get_float_arg(argvars, &f) == OK)
18934 rettv->vval.v_float = sqrt(f);
18935 else
18936 rettv->vval.v_float = 0.0;
18937}
18938
18939/*
18940 * "str2float()" function
18941 */
18942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018943f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018944{
18945 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18946
18947 if (*p == '+')
18948 p = skipwhite(p + 1);
18949 (void)string2float(p, &rettv->vval.v_float);
18950 rettv->v_type = VAR_FLOAT;
18951}
18952#endif
18953
Bram Moolenaar2c932302006-03-18 21:42:09 +000018954/*
18955 * "str2nr()" function
18956 */
18957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018958f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018959{
18960 int base = 10;
18961 char_u *p;
18962 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018963 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018964
18965 if (argvars[1].v_type != VAR_UNKNOWN)
18966 {
18967 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018968 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018969 {
18970 EMSG(_(e_invarg));
18971 return;
18972 }
18973 }
18974
18975 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018976 if (*p == '+')
18977 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018978 switch (base)
18979 {
18980 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18981 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18982 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18983 default: what = 0;
18984 }
18985 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018986 rettv->vval.v_number = n;
18987}
18988
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989#ifdef HAVE_STRFTIME
18990/*
18991 * "strftime({format}[, {time}])" function
18992 */
18993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018994f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995{
18996 char_u result_buf[256];
18997 struct tm *curtime;
18998 time_t seconds;
18999 char_u *p;
19000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019001 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019003 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019004 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 seconds = time(NULL);
19006 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019007 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 curtime = localtime(&seconds);
19009 /* MSVC returns NULL for an invalid value of seconds. */
19010 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019011 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 else
19013 {
19014# ifdef FEAT_MBYTE
19015 vimconv_T conv;
19016 char_u *enc;
19017
19018 conv.vc_type = CONV_NONE;
19019 enc = enc_locale();
19020 convert_setup(&conv, p_enc, enc);
19021 if (conv.vc_type != CONV_NONE)
19022 p = string_convert(&conv, p, NULL);
19023# endif
19024 if (p != NULL)
19025 (void)strftime((char *)result_buf, sizeof(result_buf),
19026 (char *)p, curtime);
19027 else
19028 result_buf[0] = NUL;
19029
19030# ifdef FEAT_MBYTE
19031 if (conv.vc_type != CONV_NONE)
19032 vim_free(p);
19033 convert_setup(&conv, enc, p_enc);
19034 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019035 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 else
19037# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019038 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039
19040# ifdef FEAT_MBYTE
19041 /* Release conversion descriptors */
19042 convert_setup(&conv, NULL, NULL);
19043 vim_free(enc);
19044# endif
19045 }
19046}
19047#endif
19048
19049/*
19050 * "stridx()" function
19051 */
19052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019053f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054{
19055 char_u buf[NUMBUFLEN];
19056 char_u *needle;
19057 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019058 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019060 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019062 needle = get_tv_string_chk(&argvars[1]);
19063 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019064 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019065 if (needle == NULL || haystack == NULL)
19066 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067
Bram Moolenaar33570922005-01-25 22:26:29 +000019068 if (argvars[2].v_type != VAR_UNKNOWN)
19069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019070 int error = FALSE;
19071
19072 start_idx = get_tv_number_chk(&argvars[2], &error);
19073 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019074 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019075 if (start_idx >= 0)
19076 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019077 }
19078
19079 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19080 if (pos != NULL)
19081 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082}
19083
19084/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019085 * "string()" function
19086 */
19087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019088f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019089{
19090 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019091 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019093 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019094 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019095 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019096 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019097 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098}
19099
19100/*
19101 * "strlen()" function
19102 */
19103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019104f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019106 rettv->vval.v_number = (varnumber_T)(STRLEN(
19107 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108}
19109
19110/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019111 * "strchars()" function
19112 */
19113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019114f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019115{
19116 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019117 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019118#ifdef FEAT_MBYTE
19119 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019120 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019121#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019122
19123 if (argvars[1].v_type != VAR_UNKNOWN)
19124 skipcc = get_tv_number_chk(&argvars[1], NULL);
19125 if (skipcc < 0 || skipcc > 1)
19126 EMSG(_(e_invarg));
19127 else
19128 {
19129#ifdef FEAT_MBYTE
19130 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19131 while (*s != NUL)
19132 {
19133 func_mb_ptr2char_adv(&s);
19134 ++len;
19135 }
19136 rettv->vval.v_number = len;
19137#else
19138 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19139#endif
19140 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019141}
19142
19143/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019144 * "strdisplaywidth()" function
19145 */
19146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019147f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019148{
19149 char_u *s = get_tv_string(&argvars[0]);
19150 int col = 0;
19151
19152 if (argvars[1].v_type != VAR_UNKNOWN)
19153 col = get_tv_number(&argvars[1]);
19154
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019155 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019156}
19157
19158/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019159 * "strwidth()" function
19160 */
19161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019162f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019163{
19164 char_u *s = get_tv_string(&argvars[0]);
19165
19166 rettv->vval.v_number = (varnumber_T)(
19167#ifdef FEAT_MBYTE
19168 mb_string2cells(s, -1)
19169#else
19170 STRLEN(s)
19171#endif
19172 );
19173}
19174
19175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 * "strpart()" function
19177 */
19178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019179f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180{
19181 char_u *p;
19182 int n;
19183 int len;
19184 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019185 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019187 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 slen = (int)STRLEN(p);
19189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019190 n = get_tv_number_chk(&argvars[1], &error);
19191 if (error)
19192 len = 0;
19193 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019194 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 else
19196 len = slen - n; /* default len: all bytes that are available. */
19197
19198 /*
19199 * Only return the overlap between the specified part and the actual
19200 * string.
19201 */
19202 if (n < 0)
19203 {
19204 len += n;
19205 n = 0;
19206 }
19207 else if (n > slen)
19208 n = slen;
19209 if (len < 0)
19210 len = 0;
19211 else if (n + len > slen)
19212 len = slen - n;
19213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019214 rettv->v_type = VAR_STRING;
19215 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216}
19217
19218/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019219 * "strridx()" function
19220 */
19221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019222f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019223{
19224 char_u buf[NUMBUFLEN];
19225 char_u *needle;
19226 char_u *haystack;
19227 char_u *rest;
19228 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019229 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019231 needle = get_tv_string_chk(&argvars[1]);
19232 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019233
19234 rettv->vval.v_number = -1;
19235 if (needle == NULL || haystack == NULL)
19236 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019237
19238 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019239 if (argvars[2].v_type != VAR_UNKNOWN)
19240 {
19241 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019242 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019243 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019244 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019245 }
19246 else
19247 end_idx = haystack_len;
19248
Bram Moolenaar0d660222005-01-07 21:51:51 +000019249 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019250 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019251 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019252 lastmatch = haystack + end_idx;
19253 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019254 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019255 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019256 for (rest = haystack; *rest != '\0'; ++rest)
19257 {
19258 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019259 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019260 break;
19261 lastmatch = rest;
19262 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019263 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019264
19265 if (lastmatch == NULL)
19266 rettv->vval.v_number = -1;
19267 else
19268 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19269}
19270
19271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 * "strtrans()" function
19273 */
19274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019275f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019277 rettv->v_type = VAR_STRING;
19278 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279}
19280
19281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019282 * "submatch()" function
19283 */
19284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019285f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019286{
Bram Moolenaar41571762014-04-02 19:00:58 +020019287 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019288 int no;
19289 int retList = 0;
19290
19291 no = (int)get_tv_number_chk(&argvars[0], &error);
19292 if (error)
19293 return;
19294 error = FALSE;
19295 if (argvars[1].v_type != VAR_UNKNOWN)
19296 retList = get_tv_number_chk(&argvars[1], &error);
19297 if (error)
19298 return;
19299
19300 if (retList == 0)
19301 {
19302 rettv->v_type = VAR_STRING;
19303 rettv->vval.v_string = reg_submatch(no);
19304 }
19305 else
19306 {
19307 rettv->v_type = VAR_LIST;
19308 rettv->vval.v_list = reg_submatch_list(no);
19309 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019310}
19311
19312/*
19313 * "substitute()" function
19314 */
19315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019316f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019317{
19318 char_u patbuf[NUMBUFLEN];
19319 char_u subbuf[NUMBUFLEN];
19320 char_u flagsbuf[NUMBUFLEN];
19321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019322 char_u *str = get_tv_string_chk(&argvars[0]);
19323 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19324 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19325 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19326
Bram Moolenaar0d660222005-01-07 21:51:51 +000019327 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019328 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19329 rettv->vval.v_string = NULL;
19330 else
19331 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019332}
19333
19334/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019335 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019338f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339{
19340 int id = 0;
19341#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019342 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 long col;
19344 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019345 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019347 lnum = get_tv_lnum(argvars); /* -1 on type error */
19348 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19349 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019351 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019352 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019353 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354#endif
19355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019356 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357}
19358
19359/*
19360 * "synIDattr(id, what [, mode])" function
19361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019363f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364{
19365 char_u *p = NULL;
19366#ifdef FEAT_SYN_HL
19367 int id;
19368 char_u *what;
19369 char_u *mode;
19370 char_u modebuf[NUMBUFLEN];
19371 int modec;
19372
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019373 id = get_tv_number(&argvars[0]);
19374 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019375 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019377 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019379 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 modec = 0; /* replace invalid with current */
19381 }
19382 else
19383 {
19384#ifdef FEAT_GUI
19385 if (gui.in_use)
19386 modec = 'g';
19387 else
19388#endif
19389 if (t_colors > 1)
19390 modec = 'c';
19391 else
19392 modec = 't';
19393 }
19394
19395
19396 switch (TOLOWER_ASC(what[0]))
19397 {
19398 case 'b':
19399 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19400 p = highlight_color(id, what, modec);
19401 else /* bold */
19402 p = highlight_has_attr(id, HL_BOLD, modec);
19403 break;
19404
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019405 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 p = highlight_color(id, what, modec);
19407 break;
19408
19409 case 'i':
19410 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19411 p = highlight_has_attr(id, HL_INVERSE, modec);
19412 else /* italic */
19413 p = highlight_has_attr(id, HL_ITALIC, modec);
19414 break;
19415
19416 case 'n': /* name */
19417 p = get_highlight_name(NULL, id - 1);
19418 break;
19419
19420 case 'r': /* reverse */
19421 p = highlight_has_attr(id, HL_INVERSE, modec);
19422 break;
19423
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019424 case 's':
19425 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19426 p = highlight_color(id, what, modec);
19427 else /* standout */
19428 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 break;
19430
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019431 case 'u':
19432 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19433 /* underline */
19434 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19435 else
19436 /* undercurl */
19437 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 break;
19439 }
19440
19441 if (p != NULL)
19442 p = vim_strsave(p);
19443#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019444 rettv->v_type = VAR_STRING;
19445 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446}
19447
19448/*
19449 * "synIDtrans(id)" function
19450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019452f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453{
19454 int id;
19455
19456#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019457 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458
19459 if (id > 0)
19460 id = syn_get_final_id(id);
19461 else
19462#endif
19463 id = 0;
19464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019465 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466}
19467
19468/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019469 * "synconcealed(lnum, col)" function
19470 */
19471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019472f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019473{
19474#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19475 long lnum;
19476 long col;
19477 int syntax_flags = 0;
19478 int cchar;
19479 int matchid = 0;
19480 char_u str[NUMBUFLEN];
19481#endif
19482
19483 rettv->v_type = VAR_LIST;
19484 rettv->vval.v_list = NULL;
19485
19486#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19487 lnum = get_tv_lnum(argvars); /* -1 on type error */
19488 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19489
19490 vim_memset(str, NUL, sizeof(str));
19491
19492 if (rettv_list_alloc(rettv) != FAIL)
19493 {
19494 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19495 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19496 && curwin->w_p_cole > 0)
19497 {
19498 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19499 syntax_flags = get_syntax_info(&matchid);
19500
19501 /* get the conceal character */
19502 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19503 {
19504 cchar = syn_get_sub_char();
19505 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19506 cchar = lcs_conceal;
19507 if (cchar != NUL)
19508 {
19509# ifdef FEAT_MBYTE
19510 if (has_mbyte)
19511 (*mb_char2bytes)(cchar, str);
19512 else
19513# endif
19514 str[0] = cchar;
19515 }
19516 }
19517 }
19518
19519 list_append_number(rettv->vval.v_list,
19520 (syntax_flags & HL_CONCEAL) != 0);
19521 /* -1 to auto-determine strlen */
19522 list_append_string(rettv->vval.v_list, str, -1);
19523 list_append_number(rettv->vval.v_list, matchid);
19524 }
19525#endif
19526}
19527
19528/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019529 * "synstack(lnum, col)" function
19530 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019532f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019533{
19534#ifdef FEAT_SYN_HL
19535 long lnum;
19536 long col;
19537 int i;
19538 int id;
19539#endif
19540
19541 rettv->v_type = VAR_LIST;
19542 rettv->vval.v_list = NULL;
19543
19544#ifdef FEAT_SYN_HL
19545 lnum = get_tv_lnum(argvars); /* -1 on type error */
19546 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19547
19548 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019549 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019550 && rettv_list_alloc(rettv) != FAIL)
19551 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019552 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019553 for (i = 0; ; ++i)
19554 {
19555 id = syn_get_stack_item(i);
19556 if (id < 0)
19557 break;
19558 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19559 break;
19560 }
19561 }
19562#endif
19563}
19564
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019566get_cmd_output_as_rettv(
19567 typval_T *argvars,
19568 typval_T *rettv,
19569 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019571 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019573 char_u *infile = NULL;
19574 char_u buf[NUMBUFLEN];
19575 int err = FALSE;
19576 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019577 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019578 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019580 rettv->v_type = VAR_STRING;
19581 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019582 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019583 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019584
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019585 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019586 {
19587 /*
19588 * Write the string to a temp file, to be used for input of the shell
19589 * command.
19590 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019591 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019592 {
19593 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019594 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019595 }
19596
19597 fd = mch_fopen((char *)infile, WRITEBIN);
19598 if (fd == NULL)
19599 {
19600 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019601 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019602 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019603 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019604 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019605 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19606 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019607 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019608 else
19609 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019610 size_t len;
19611
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019612 p = get_tv_string_buf_chk(&argvars[1], buf);
19613 if (p == NULL)
19614 {
19615 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019616 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019617 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019618 len = STRLEN(p);
19619 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019620 err = TRUE;
19621 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019622 if (fclose(fd) != 0)
19623 err = TRUE;
19624 if (err)
19625 {
19626 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019627 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019628 }
19629 }
19630
Bram Moolenaar52a72462014-08-29 15:53:52 +020019631 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19632 * echoes typeahead, that messes up the display. */
19633 if (!msg_silent)
19634 flags += SHELL_COOKED;
19635
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019636 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019638 int len;
19639 listitem_T *li;
19640 char_u *s = NULL;
19641 char_u *start;
19642 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019643 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644
Bram Moolenaar52a72462014-08-29 15:53:52 +020019645 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019646 if (res == NULL)
19647 goto errret;
19648
19649 list = list_alloc();
19650 if (list == NULL)
19651 goto errret;
19652
19653 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019655 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019656 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019657 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019658 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019659
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019660 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019661 if (s == NULL)
19662 goto errret;
19663
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019664 for (p = s; start < end; ++p, ++start)
19665 *p = *start == NUL ? NL : *start;
19666 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019667
19668 li = listitem_alloc();
19669 if (li == NULL)
19670 {
19671 vim_free(s);
19672 goto errret;
19673 }
19674 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019675 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019676 li->li_tv.vval.v_string = s;
19677 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019679
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019680 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019681 rettv->v_type = VAR_LIST;
19682 rettv->vval.v_list = list;
19683 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019685 else
19686 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019687 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019688#ifdef USE_CR
19689 /* translate <CR> into <NL> */
19690 if (res != NULL)
19691 {
19692 char_u *s;
19693
19694 for (s = res; *s; ++s)
19695 {
19696 if (*s == CAR)
19697 *s = NL;
19698 }
19699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700#else
19701# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019702 /* translate <CR><NL> into <NL> */
19703 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019705 char_u *s, *d;
19706
19707 d = res;
19708 for (s = res; *s; ++s)
19709 {
19710 if (s[0] == CAR && s[1] == NL)
19711 ++s;
19712 *d++ = *s;
19713 }
19714 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716# endif
19717#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019718 rettv->vval.v_string = res;
19719 res = NULL;
19720 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019721
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019722errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019723 if (infile != NULL)
19724 {
19725 mch_remove(infile);
19726 vim_free(infile);
19727 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019728 if (res != NULL)
19729 vim_free(res);
19730 if (list != NULL)
19731 list_free(list, TRUE);
19732}
19733
19734/*
19735 * "system()" function
19736 */
19737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019738f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019739{
19740 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19741}
19742
19743/*
19744 * "systemlist()" function
19745 */
19746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019747f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019748{
19749 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750}
19751
19752/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019753 * "tabpagebuflist()" function
19754 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019756f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019757{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019758#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019759 tabpage_T *tp;
19760 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019761
19762 if (argvars[0].v_type == VAR_UNKNOWN)
19763 wp = firstwin;
19764 else
19765 {
19766 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19767 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019768 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019769 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019770 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019771 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019772 for (; wp != NULL; wp = wp->w_next)
19773 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019774 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019775 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019776 }
19777#endif
19778}
19779
19780
19781/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019782 * "tabpagenr()" function
19783 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019785f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019786{
19787 int nr = 1;
19788#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019789 char_u *arg;
19790
19791 if (argvars[0].v_type != VAR_UNKNOWN)
19792 {
19793 arg = get_tv_string_chk(&argvars[0]);
19794 nr = 0;
19795 if (arg != NULL)
19796 {
19797 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019798 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019799 else
19800 EMSG2(_(e_invexpr2), arg);
19801 }
19802 }
19803 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019804 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019805#endif
19806 rettv->vval.v_number = nr;
19807}
19808
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019809
19810#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019811static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019812
19813/*
19814 * Common code for tabpagewinnr() and winnr().
19815 */
19816 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019817get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019818{
19819 win_T *twin;
19820 int nr = 1;
19821 win_T *wp;
19822 char_u *arg;
19823
19824 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19825 if (argvar->v_type != VAR_UNKNOWN)
19826 {
19827 arg = get_tv_string_chk(argvar);
19828 if (arg == NULL)
19829 nr = 0; /* type error; errmsg already given */
19830 else if (STRCMP(arg, "$") == 0)
19831 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19832 else if (STRCMP(arg, "#") == 0)
19833 {
19834 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19835 if (twin == NULL)
19836 nr = 0;
19837 }
19838 else
19839 {
19840 EMSG2(_(e_invexpr2), arg);
19841 nr = 0;
19842 }
19843 }
19844
19845 if (nr > 0)
19846 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19847 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019848 {
19849 if (wp == NULL)
19850 {
19851 /* didn't find it in this tabpage */
19852 nr = 0;
19853 break;
19854 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019855 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019856 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019857 return nr;
19858}
19859#endif
19860
19861/*
19862 * "tabpagewinnr()" function
19863 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019865f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019866{
19867 int nr = 1;
19868#ifdef FEAT_WINDOWS
19869 tabpage_T *tp;
19870
19871 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19872 if (tp == NULL)
19873 nr = 0;
19874 else
19875 nr = get_winnr(tp, &argvars[1]);
19876#endif
19877 rettv->vval.v_number = nr;
19878}
19879
19880
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019881/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019882 * "tagfiles()" function
19883 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019885f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019886{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019887 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019888 tagname_T tn;
19889 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019890
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019891 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019892 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019893 fname = alloc(MAXPATHL);
19894 if (fname == NULL)
19895 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019896
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019897 for (first = TRUE; ; first = FALSE)
19898 if (get_tagfname(&tn, first, fname) == FAIL
19899 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019900 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019901 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019902 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019903}
19904
19905/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019906 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019907 */
19908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019909f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019910{
19911 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019912
19913 tag_pattern = get_tv_string(&argvars[0]);
19914
19915 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019916 if (*tag_pattern == NUL)
19917 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019918
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019919 if (rettv_list_alloc(rettv) == OK)
19920 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019921}
19922
19923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 * "tempname()" function
19925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019927f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928{
19929 static int x = 'A';
19930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019931 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019932 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933
19934 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19935 * names. Skip 'I' and 'O', they are used for shell redirection. */
19936 do
19937 {
19938 if (x == 'Z')
19939 x = '0';
19940 else if (x == '9')
19941 x = 'A';
19942 else
19943 {
19944#ifdef EBCDIC
19945 if (x == 'I')
19946 x = 'J';
19947 else if (x == 'R')
19948 x = 'S';
19949 else
19950#endif
19951 ++x;
19952 }
19953 } while (x == 'I' || x == 'O');
19954}
19955
19956/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019957 * "test(list)" function: Just checking the walls...
19958 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019960f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019961{
19962 /* Used for unit testing. Change the code below to your liking. */
19963#if 0
19964 listitem_T *li;
19965 list_T *l;
19966 char_u *bad, *good;
19967
19968 if (argvars[0].v_type != VAR_LIST)
19969 return;
19970 l = argvars[0].vval.v_list;
19971 if (l == NULL)
19972 return;
19973 li = l->lv_first;
19974 if (li == NULL)
19975 return;
19976 bad = get_tv_string(&li->li_tv);
19977 li = li->li_next;
19978 if (li == NULL)
19979 return;
19980 good = get_tv_string(&li->li_tv);
19981 rettv->vval.v_number = test_edit_score(bad, good);
19982#endif
19983}
19984
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019985#ifdef FEAT_FLOAT
19986/*
19987 * "tan()" function
19988 */
19989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019990f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019991{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019992 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019993
19994 rettv->v_type = VAR_FLOAT;
19995 if (get_float_arg(argvars, &f) == OK)
19996 rettv->vval.v_float = tan(f);
19997 else
19998 rettv->vval.v_float = 0.0;
19999}
20000
20001/*
20002 * "tanh()" function
20003 */
20004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020005f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020006{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020007 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020008
20009 rettv->v_type = VAR_FLOAT;
20010 if (get_float_arg(argvars, &f) == OK)
20011 rettv->vval.v_float = tanh(f);
20012 else
20013 rettv->vval.v_float = 0.0;
20014}
20015#endif
20016
Bram Moolenaard52d9742005-08-21 22:20:28 +000020017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 * "tolower(string)" function
20019 */
20020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020021f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022{
20023 char_u *p;
20024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020025 p = vim_strsave(get_tv_string(&argvars[0]));
20026 rettv->v_type = VAR_STRING;
20027 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028
20029 if (p != NULL)
20030 while (*p != NUL)
20031 {
20032#ifdef FEAT_MBYTE
20033 int l;
20034
20035 if (enc_utf8)
20036 {
20037 int c, lc;
20038
20039 c = utf_ptr2char(p);
20040 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020041 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 /* TODO: reallocate string when byte count changes. */
20043 if (utf_char2len(lc) == l)
20044 utf_char2bytes(lc, p);
20045 p += l;
20046 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020047 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 p += l; /* skip multi-byte character */
20049 else
20050#endif
20051 {
20052 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20053 ++p;
20054 }
20055 }
20056}
20057
20058/*
20059 * "toupper(string)" function
20060 */
20061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020062f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020064 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020065 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066}
20067
20068/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020069 * "tr(string, fromstr, tostr)" function
20070 */
20071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020072f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020073{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020074 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020075 char_u *fromstr;
20076 char_u *tostr;
20077 char_u *p;
20078#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020079 int inlen;
20080 int fromlen;
20081 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020082 int idx;
20083 char_u *cpstr;
20084 int cplen;
20085 int first = TRUE;
20086#endif
20087 char_u buf[NUMBUFLEN];
20088 char_u buf2[NUMBUFLEN];
20089 garray_T ga;
20090
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020091 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020092 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20093 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020094
20095 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 rettv->v_type = VAR_STRING;
20097 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020098 if (fromstr == NULL || tostr == NULL)
20099 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020100 ga_init2(&ga, (int)sizeof(char), 80);
20101
20102#ifdef FEAT_MBYTE
20103 if (!has_mbyte)
20104#endif
20105 /* not multi-byte: fromstr and tostr must be the same length */
20106 if (STRLEN(fromstr) != STRLEN(tostr))
20107 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020108#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020109error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020110#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020111 EMSG2(_(e_invarg2), fromstr);
20112 ga_clear(&ga);
20113 return;
20114 }
20115
20116 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020117 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020118 {
20119#ifdef FEAT_MBYTE
20120 if (has_mbyte)
20121 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020122 inlen = (*mb_ptr2len)(in_str);
20123 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020124 cplen = inlen;
20125 idx = 0;
20126 for (p = fromstr; *p != NUL; p += fromlen)
20127 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020128 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020129 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020130 {
20131 for (p = tostr; *p != NUL; p += tolen)
20132 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020133 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020134 if (idx-- == 0)
20135 {
20136 cplen = tolen;
20137 cpstr = p;
20138 break;
20139 }
20140 }
20141 if (*p == NUL) /* tostr is shorter than fromstr */
20142 goto error;
20143 break;
20144 }
20145 ++idx;
20146 }
20147
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020148 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020149 {
20150 /* Check that fromstr and tostr have the same number of
20151 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020152 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020153 first = FALSE;
20154 for (p = tostr; *p != NUL; p += tolen)
20155 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020156 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020157 --idx;
20158 }
20159 if (idx != 0)
20160 goto error;
20161 }
20162
Bram Moolenaarcde88542015-08-11 19:14:00 +020020163 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020164 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020165 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020166
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020167 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020168 }
20169 else
20170#endif
20171 {
20172 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020173 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020174 if (p != NULL)
20175 ga_append(&ga, tostr[p - fromstr]);
20176 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020177 ga_append(&ga, *in_str);
20178 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020179 }
20180 }
20181
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020182 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020183 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020184 ga_append(&ga, NUL);
20185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020186 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020187}
20188
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020189#ifdef FEAT_FLOAT
20190/*
20191 * "trunc({float})" function
20192 */
20193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020194f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020195{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020196 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020197
20198 rettv->v_type = VAR_FLOAT;
20199 if (get_float_arg(argvars, &f) == OK)
20200 /* trunc() is not in C90, use floor() or ceil() instead. */
20201 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20202 else
20203 rettv->vval.v_float = 0.0;
20204}
20205#endif
20206
Bram Moolenaar8299df92004-07-10 09:47:34 +000020207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 * "type(expr)" function
20209 */
20210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020211f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020213 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020214
20215 switch (argvars[0].v_type)
20216 {
20217 case VAR_NUMBER: n = 0; break;
20218 case VAR_STRING: n = 1; break;
20219 case VAR_FUNC: n = 2; break;
20220 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020221 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020222 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020223 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020224 if (argvars[0].vval.v_number == VVAL_FALSE
20225 || argvars[0].vval.v_number == VVAL_TRUE)
20226 n = 6;
20227 else
20228 n = 7;
20229 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020230 case VAR_JOB: n = 8; break;
20231 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020232 case VAR_UNKNOWN:
20233 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20234 n = -1;
20235 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020236 }
20237 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238}
20239
20240/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020241 * "undofile(name)" function
20242 */
20243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020244f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020245{
20246 rettv->v_type = VAR_STRING;
20247#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020248 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020249 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020250
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020251 if (*fname == NUL)
20252 {
20253 /* If there is no file name there will be no undo file. */
20254 rettv->vval.v_string = NULL;
20255 }
20256 else
20257 {
20258 char_u *ffname = FullName_save(fname, FALSE);
20259
20260 if (ffname != NULL)
20261 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20262 vim_free(ffname);
20263 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020264 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020265#else
20266 rettv->vval.v_string = NULL;
20267#endif
20268}
20269
20270/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020271 * "undotree()" function
20272 */
20273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020274f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020275{
20276 if (rettv_dict_alloc(rettv) == OK)
20277 {
20278 dict_T *dict = rettv->vval.v_dict;
20279 list_T *list;
20280
Bram Moolenaar730cde92010-06-27 05:18:54 +020020281 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020282 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020283 dict_add_nr_str(dict, "save_last",
20284 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020285 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20286 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020287 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020288
20289 list = list_alloc();
20290 if (list != NULL)
20291 {
20292 u_eval_tree(curbuf->b_u_oldhead, list);
20293 dict_add_list(dict, "entries", list);
20294 }
20295 }
20296}
20297
20298/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020299 * "values(dict)" function
20300 */
20301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020302f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020303{
20304 dict_list(argvars, rettv, 1);
20305}
20306
20307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 * "virtcol(string)" function
20309 */
20310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020311f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312{
20313 colnr_T vcol = 0;
20314 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020315 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020317 fp = var2fpos(&argvars[0], FALSE, &fnum);
20318 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20319 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 {
20321 getvvcol(curwin, fp, NULL, NULL, &vcol);
20322 ++vcol;
20323 }
20324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020325 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326}
20327
20328/*
20329 * "visualmode()" function
20330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020332f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 char_u str[2];
20335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020336 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 str[0] = curbuf->b_visual_mode_eval;
20338 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020339 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340
20341 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020342 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344}
20345
20346/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020347 * "wildmenumode()" function
20348 */
20349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020350f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020351{
20352#ifdef FEAT_WILDMENU
20353 if (wild_menu_showing)
20354 rettv->vval.v_number = 1;
20355#endif
20356}
20357
20358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 * "winbufnr(nr)" function
20360 */
20361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020362f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363{
20364 win_T *wp;
20365
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020366 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020370 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371}
20372
20373/*
20374 * "wincol()" function
20375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020377f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378{
20379 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020380 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381}
20382
20383/*
20384 * "winheight(nr)" function
20385 */
20386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020387f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388{
20389 win_T *wp;
20390
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020391 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020393 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020395 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396}
20397
20398/*
20399 * "winline()" function
20400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020402f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403{
20404 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020405 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406}
20407
20408/*
20409 * "winnr()" function
20410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020412f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413{
20414 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020415
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020417 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020419 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420}
20421
20422/*
20423 * "winrestcmd()" function
20424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020426f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427{
20428#ifdef FEAT_WINDOWS
20429 win_T *wp;
20430 int winnr = 1;
20431 garray_T ga;
20432 char_u buf[50];
20433
20434 ga_init2(&ga, (int)sizeof(char), 70);
20435 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20436 {
20437 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20438 ga_concat(&ga, buf);
20439# ifdef FEAT_VERTSPLIT
20440 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20441 ga_concat(&ga, buf);
20442# endif
20443 ++winnr;
20444 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020445 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020447 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020449 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020451 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452}
20453
20454/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020455 * "winrestview()" function
20456 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020458f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020459{
20460 dict_T *dict;
20461
20462 if (argvars[0].v_type != VAR_DICT
20463 || (dict = argvars[0].vval.v_dict) == NULL)
20464 EMSG(_(e_invarg));
20465 else
20466 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020467 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20468 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20469 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20470 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020471#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020472 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20473 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020474#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020475 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20476 {
20477 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20478 curwin->w_set_curswant = FALSE;
20479 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020480
Bram Moolenaar82c25852014-05-28 16:47:16 +020020481 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20482 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020483#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020484 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20485 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020486#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020487 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20488 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20489 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20490 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020491
20492 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020493 win_new_height(curwin, curwin->w_height);
20494# ifdef FEAT_VERTSPLIT
20495 win_new_width(curwin, W_WIDTH(curwin));
20496# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020497 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020498
Bram Moolenaarb851a962014-10-31 15:45:52 +010020499 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020500 curwin->w_topline = 1;
20501 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20502 curwin->w_topline = curbuf->b_ml.ml_line_count;
20503#ifdef FEAT_DIFF
20504 check_topfill(curwin, TRUE);
20505#endif
20506 }
20507}
20508
20509/*
20510 * "winsaveview()" function
20511 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020513f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020514{
20515 dict_T *dict;
20516
Bram Moolenaara800b422010-06-27 01:15:55 +020020517 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020518 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020519 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020520
20521 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20522 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20523#ifdef FEAT_VIRTUALEDIT
20524 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20525#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020526 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020527 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20528
20529 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20530#ifdef FEAT_DIFF
20531 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20532#endif
20533 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20534 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20535}
20536
20537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 * "winwidth(nr)" function
20539 */
20540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020541f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542{
20543 win_T *wp;
20544
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020545 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020547 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548 else
20549#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020550 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020552 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553#endif
20554}
20555
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020557 * "wordcount()" function
20558 */
20559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020560f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020561{
20562 if (rettv_dict_alloc(rettv) == FAIL)
20563 return;
20564 cursor_pos_info(rettv->vval.v_dict);
20565}
20566
20567/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020568 * Write list of strings to file
20569 */
20570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020571write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020572{
20573 listitem_T *li;
20574 int c;
20575 int ret = OK;
20576 char_u *s;
20577
20578 for (li = list->lv_first; li != NULL; li = li->li_next)
20579 {
20580 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20581 {
20582 if (*s == '\n')
20583 c = putc(NUL, fd);
20584 else
20585 c = putc(*s, fd);
20586 if (c == EOF)
20587 {
20588 ret = FAIL;
20589 break;
20590 }
20591 }
20592 if (!binary || li->li_next != NULL)
20593 if (putc('\n', fd) == EOF)
20594 {
20595 ret = FAIL;
20596 break;
20597 }
20598 if (ret == FAIL)
20599 {
20600 EMSG(_(e_write));
20601 break;
20602 }
20603 }
20604 return ret;
20605}
20606
20607/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020608 * "writefile()" function
20609 */
20610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020611f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020612{
20613 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020614 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020615 char_u *fname;
20616 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020617 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020618
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020619 if (check_restricted() || check_secure())
20620 return;
20621
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020622 if (argvars[0].v_type != VAR_LIST)
20623 {
20624 EMSG2(_(e_listarg), "writefile()");
20625 return;
20626 }
20627 if (argvars[0].vval.v_list == NULL)
20628 return;
20629
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020630 if (argvars[2].v_type != VAR_UNKNOWN)
20631 {
20632 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20633 binary = TRUE;
20634 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20635 append = TRUE;
20636 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020637
20638 /* Always open the file in binary mode, library functions have a mind of
20639 * their own about CR-LF conversion. */
20640 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020641 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20642 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020643 {
20644 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20645 ret = -1;
20646 }
20647 else
20648 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020649 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20650 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020651 fclose(fd);
20652 }
20653
20654 rettv->vval.v_number = ret;
20655}
20656
20657/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020658 * "xor(expr, expr)" function
20659 */
20660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020661f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020662{
20663 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20664 ^ get_tv_number_chk(&argvars[1], NULL);
20665}
20666
20667
20668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020670 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 */
20672 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020673var2fpos(
20674 typval_T *varp,
20675 int dollar_lnum, /* TRUE when $ is last line */
20676 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020678 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020680 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681
Bram Moolenaara5525202006-03-02 22:52:09 +000020682 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020683 if (varp->v_type == VAR_LIST)
20684 {
20685 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020686 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020687 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020688 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020689
20690 l = varp->vval.v_list;
20691 if (l == NULL)
20692 return NULL;
20693
20694 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020695 pos.lnum = list_find_nr(l, 0L, &error);
20696 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020697 return NULL; /* invalid line number */
20698
20699 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020700 pos.col = list_find_nr(l, 1L, &error);
20701 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020702 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020703 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020704
20705 /* We accept "$" for the column number: last column. */
20706 li = list_find(l, 1L);
20707 if (li != NULL && li->li_tv.v_type == VAR_STRING
20708 && li->li_tv.vval.v_string != NULL
20709 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20710 pos.col = len + 1;
20711
Bram Moolenaara5525202006-03-02 22:52:09 +000020712 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020713 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020714 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020715 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020716
Bram Moolenaara5525202006-03-02 22:52:09 +000020717#ifdef FEAT_VIRTUALEDIT
20718 /* Get the virtual offset. Defaults to zero. */
20719 pos.coladd = list_find_nr(l, 2L, &error);
20720 if (error)
20721 pos.coladd = 0;
20722#endif
20723
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020724 return &pos;
20725 }
20726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020727 name = get_tv_string_chk(varp);
20728 if (name == NULL)
20729 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020730 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020732 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20733 {
20734 if (VIsual_active)
20735 return &VIsual;
20736 return &curwin->w_cursor;
20737 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020738 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020740 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20742 return NULL;
20743 return pp;
20744 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020745
20746#ifdef FEAT_VIRTUALEDIT
20747 pos.coladd = 0;
20748#endif
20749
Bram Moolenaar477933c2007-07-17 14:32:23 +000020750 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020751 {
20752 pos.col = 0;
20753 if (name[1] == '0') /* "w0": first visible line */
20754 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020755 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020756 pos.lnum = curwin->w_topline;
20757 return &pos;
20758 }
20759 else if (name[1] == '$') /* "w$": last visible line */
20760 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020761 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020762 pos.lnum = curwin->w_botline - 1;
20763 return &pos;
20764 }
20765 }
20766 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020768 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 {
20770 pos.lnum = curbuf->b_ml.ml_line_count;
20771 pos.col = 0;
20772 }
20773 else
20774 {
20775 pos.lnum = curwin->w_cursor.lnum;
20776 pos.col = (colnr_T)STRLEN(ml_get_curline());
20777 }
20778 return &pos;
20779 }
20780 return NULL;
20781}
20782
20783/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020784 * Convert list in "arg" into a position and optional file number.
20785 * When "fnump" is NULL there is no file number, only 3 items.
20786 * Note that the column is passed on as-is, the caller may want to decrement
20787 * it to use 1 for the first column.
20788 * Return FAIL when conversion is not possible, doesn't check the position for
20789 * validity.
20790 */
20791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020792list2fpos(
20793 typval_T *arg,
20794 pos_T *posp,
20795 int *fnump,
20796 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020797{
20798 list_T *l = arg->vval.v_list;
20799 long i = 0;
20800 long n;
20801
Bram Moolenaar493c1782014-05-28 14:34:46 +020020802 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20803 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020804 if (arg->v_type != VAR_LIST
20805 || l == NULL
20806 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020807 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020808 return FAIL;
20809
20810 if (fnump != NULL)
20811 {
20812 n = list_find_nr(l, i++, NULL); /* fnum */
20813 if (n < 0)
20814 return FAIL;
20815 if (n == 0)
20816 n = curbuf->b_fnum; /* current buffer */
20817 *fnump = n;
20818 }
20819
20820 n = list_find_nr(l, i++, NULL); /* lnum */
20821 if (n < 0)
20822 return FAIL;
20823 posp->lnum = n;
20824
20825 n = list_find_nr(l, i++, NULL); /* col */
20826 if (n < 0)
20827 return FAIL;
20828 posp->col = n;
20829
20830#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020831 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020832 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020833 posp->coladd = 0;
20834 else
20835 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020836#endif
20837
Bram Moolenaar493c1782014-05-28 14:34:46 +020020838 if (curswantp != NULL)
20839 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20840
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020841 return OK;
20842}
20843
20844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 * Get the length of an environment variable name.
20846 * Advance "arg" to the first character after the name.
20847 * Return 0 for error.
20848 */
20849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020850get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851{
20852 char_u *p;
20853 int len;
20854
20855 for (p = *arg; vim_isIDc(*p); ++p)
20856 ;
20857 if (p == *arg) /* no name found */
20858 return 0;
20859
20860 len = (int)(p - *arg);
20861 *arg = p;
20862 return len;
20863}
20864
20865/*
20866 * Get the length of the name of a function or internal variable.
20867 * "arg" is advanced to the first non-white character after the name.
20868 * Return 0 if something is wrong.
20869 */
20870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020871get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872{
20873 char_u *p;
20874 int len;
20875
20876 /* Find the end of the name. */
20877 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020878 {
20879 if (*p == ':')
20880 {
20881 /* "s:" is start of "s:var", but "n:" is not and can be used in
20882 * slice "[n:]". Also "xx:" is not a namespace. */
20883 len = (int)(p - *arg);
20884 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20885 || len > 1)
20886 break;
20887 }
20888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 if (p == *arg) /* no name found */
20890 return 0;
20891
20892 len = (int)(p - *arg);
20893 *arg = skipwhite(p);
20894
20895 return len;
20896}
20897
20898/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020899 * Get the length of the name of a variable or function.
20900 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020902 * Return -1 if curly braces expansion failed.
20903 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 * If the name contains 'magic' {}'s, expand them and return the
20905 * expanded name in an allocated string via 'alias' - caller must free.
20906 */
20907 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020908get_name_len(
20909 char_u **arg,
20910 char_u **alias,
20911 int evaluate,
20912 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913{
20914 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 char_u *p;
20916 char_u *expr_start;
20917 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918
20919 *alias = NULL; /* default to no alias */
20920
20921 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20922 && (*arg)[2] == (int)KE_SNR)
20923 {
20924 /* hard coded <SNR>, already translated */
20925 *arg += 3;
20926 return get_id_len(arg) + 3;
20927 }
20928 len = eval_fname_script(*arg);
20929 if (len > 0)
20930 {
20931 /* literal "<SID>", "s:" or "<SNR>" */
20932 *arg += len;
20933 }
20934
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020936 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020938 p = find_name_end(*arg, &expr_start, &expr_end,
20939 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 if (expr_start != NULL)
20941 {
20942 char_u *temp_string;
20943
20944 if (!evaluate)
20945 {
20946 len += (int)(p - *arg);
20947 *arg = skipwhite(p);
20948 return len;
20949 }
20950
20951 /*
20952 * Include any <SID> etc in the expanded string:
20953 * Thus the -len here.
20954 */
20955 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20956 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020957 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 *alias = temp_string;
20959 *arg = skipwhite(p);
20960 return (int)STRLEN(temp_string);
20961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962
20963 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020964 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 EMSG2(_(e_invexpr2), *arg);
20966
20967 return len;
20968}
20969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020970/*
20971 * Find the end of a variable or function name, taking care of magic braces.
20972 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20973 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020974 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020975 * Return a pointer to just after the name. Equal to "arg" if there is no
20976 * valid name.
20977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020979find_name_end(
20980 char_u *arg,
20981 char_u **expr_start,
20982 char_u **expr_end,
20983 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020985 int mb_nest = 0;
20986 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020988 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020990 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020992 *expr_start = NULL;
20993 *expr_end = NULL;
20994 }
20995
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020996 /* Quick check for valid starting character. */
20997 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20998 return arg;
20999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021000 for (p = arg; *p != NUL
21001 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021002 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021003 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021004 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021005 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021006 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021007 if (*p == '\'')
21008 {
21009 /* skip over 'string' to avoid counting [ and ] inside it. */
21010 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21011 ;
21012 if (*p == NUL)
21013 break;
21014 }
21015 else if (*p == '"')
21016 {
21017 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21018 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21019 if (*p == '\\' && p[1] != NUL)
21020 ++p;
21021 if (*p == NUL)
21022 break;
21023 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021024 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21025 {
21026 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021027 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021028 len = (int)(p - arg);
21029 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021030 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021031 break;
21032 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021034 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021036 if (*p == '[')
21037 ++br_nest;
21038 else if (*p == ']')
21039 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021042 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021044 if (*p == '{')
21045 {
21046 mb_nest++;
21047 if (expr_start != NULL && *expr_start == NULL)
21048 *expr_start = p;
21049 }
21050 else if (*p == '}')
21051 {
21052 mb_nest--;
21053 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21054 *expr_end = p;
21055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057 }
21058
21059 return p;
21060}
21061
21062/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021063 * Expands out the 'magic' {}'s in a variable/function name.
21064 * Note that this can call itself recursively, to deal with
21065 * constructs like foo{bar}{baz}{bam}
21066 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21067 * "in_start" ^
21068 * "expr_start" ^
21069 * "expr_end" ^
21070 * "in_end" ^
21071 *
21072 * Returns a new allocated string, which the caller must free.
21073 * Returns NULL for failure.
21074 */
21075 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021076make_expanded_name(
21077 char_u *in_start,
21078 char_u *expr_start,
21079 char_u *expr_end,
21080 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021081{
21082 char_u c1;
21083 char_u *retval = NULL;
21084 char_u *temp_result;
21085 char_u *nextcmd = NULL;
21086
21087 if (expr_end == NULL || in_end == NULL)
21088 return NULL;
21089 *expr_start = NUL;
21090 *expr_end = NUL;
21091 c1 = *in_end;
21092 *in_end = NUL;
21093
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021094 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021095 if (temp_result != NULL && nextcmd == NULL)
21096 {
21097 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21098 + (in_end - expr_end) + 1));
21099 if (retval != NULL)
21100 {
21101 STRCPY(retval, in_start);
21102 STRCAT(retval, temp_result);
21103 STRCAT(retval, expr_end + 1);
21104 }
21105 }
21106 vim_free(temp_result);
21107
21108 *in_end = c1; /* put char back for error messages */
21109 *expr_start = '{';
21110 *expr_end = '}';
21111
21112 if (retval != NULL)
21113 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021114 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021115 if (expr_start != NULL)
21116 {
21117 /* Further expansion! */
21118 temp_result = make_expanded_name(retval, expr_start,
21119 expr_end, temp_result);
21120 vim_free(retval);
21121 retval = temp_result;
21122 }
21123 }
21124
21125 return retval;
21126}
21127
21128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021130 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 */
21132 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021133eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021135 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21136}
21137
21138/*
21139 * Return TRUE if character "c" can be used as the first character in a
21140 * variable or function name (excluding '{' and '}').
21141 */
21142 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021143eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021144{
21145 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146}
21147
21148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 * Set number v: variable to "val".
21150 */
21151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021152set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021154 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155}
21156
21157/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021158 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 */
21160 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021161get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021163 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164}
21165
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021166/*
21167 * Get string v: variable value. Uses a static buffer, can only be used once.
21168 */
21169 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021170get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021171{
21172 return get_tv_string(&vimvars[idx].vv_tv);
21173}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021174
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021176 * Get List v: variable value. Caller must take care of reference count when
21177 * needed.
21178 */
21179 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021180get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021181{
21182 return vimvars[idx].vv_list;
21183}
21184
21185/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021186 * Set v:char to character "c".
21187 */
21188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021189set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021190{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021191 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021192
21193#ifdef FEAT_MBYTE
21194 if (has_mbyte)
21195 buf[(*mb_char2bytes)(c, buf)] = NUL;
21196 else
21197#endif
21198 {
21199 buf[0] = c;
21200 buf[1] = NUL;
21201 }
21202 set_vim_var_string(VV_CHAR, buf, -1);
21203}
21204
21205/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021206 * Set v:count to "count" and v:count1 to "count1".
21207 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208 */
21209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210set_vcount(
21211 long count,
21212 long count1,
21213 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021215 if (set_prevcount)
21216 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021217 vimvars[VV_COUNT].vv_nr = count;
21218 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219}
21220
21221/*
21222 * Set string v: variable to a copy of "val".
21223 */
21224 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021225set_vim_var_string(
21226 int idx,
21227 char_u *val,
21228 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229{
Bram Moolenaara542c682016-01-31 16:28:04 +010021230 clear_tv(&vimvars[idx].vv_di.di_tv);
21231 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021233 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021235 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021237 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238}
21239
21240/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021241 * Set List v: variable to "val".
21242 */
21243 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021244set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021245{
Bram Moolenaara542c682016-01-31 16:28:04 +010021246 clear_tv(&vimvars[idx].vv_di.di_tv);
21247 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021248 vimvars[idx].vv_list = val;
21249 if (val != NULL)
21250 ++val->lv_refcount;
21251}
21252
21253/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021254 * Set Dictionary v: variable to "val".
21255 */
21256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021257set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021258{
21259 int todo;
21260 hashitem_T *hi;
21261
Bram Moolenaara542c682016-01-31 16:28:04 +010021262 clear_tv(&vimvars[idx].vv_di.di_tv);
21263 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021264 vimvars[idx].vv_dict = val;
21265 if (val != NULL)
21266 {
21267 ++val->dv_refcount;
21268
21269 /* Set readonly */
21270 todo = (int)val->dv_hashtab.ht_used;
21271 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21272 {
21273 if (HASHITEM_EMPTY(hi))
21274 continue;
21275 --todo;
21276 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21277 }
21278 }
21279}
21280
21281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 * Set v:register if needed.
21283 */
21284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021285set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286{
21287 char_u regname;
21288
21289 if (c == 0 || c == ' ')
21290 regname = '"';
21291 else
21292 regname = c;
21293 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021294 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 set_vim_var_string(VV_REG, &regname, 1);
21296}
21297
21298/*
21299 * Get or set v:exception. If "oldval" == NULL, return the current value.
21300 * Otherwise, restore the value to "oldval" and return NULL.
21301 * Must always be called in pairs to save and restore v:exception! Does not
21302 * take care of memory allocations.
21303 */
21304 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021305v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306{
21307 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021308 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309
Bram Moolenaare9a41262005-01-15 22:18:47 +000021310 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 return NULL;
21312}
21313
21314/*
21315 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21316 * Otherwise, restore the value to "oldval" and return NULL.
21317 * Must always be called in pairs to save and restore v:throwpoint! Does not
21318 * take care of memory allocations.
21319 */
21320 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021321v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322{
21323 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021324 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325
Bram Moolenaare9a41262005-01-15 22:18:47 +000021326 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 return NULL;
21328}
21329
21330#if defined(FEAT_AUTOCMD) || defined(PROTO)
21331/*
21332 * Set v:cmdarg.
21333 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21334 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21335 * Must always be called in pairs!
21336 */
21337 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021338set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339{
21340 char_u *oldval;
21341 char_u *newval;
21342 unsigned len;
21343
Bram Moolenaare9a41262005-01-15 22:18:47 +000021344 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021345 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021347 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021348 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021349 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 }
21351
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021352 if (eap->force_bin == FORCE_BIN)
21353 len = 6;
21354 else if (eap->force_bin == FORCE_NOBIN)
21355 len = 8;
21356 else
21357 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021358
21359 if (eap->read_edit)
21360 len += 7;
21361
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021362 if (eap->force_ff != 0)
21363 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21364# ifdef FEAT_MBYTE
21365 if (eap->force_enc != 0)
21366 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021367 if (eap->bad_char != 0)
21368 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021369# endif
21370
21371 newval = alloc(len + 1);
21372 if (newval == NULL)
21373 return NULL;
21374
21375 if (eap->force_bin == FORCE_BIN)
21376 sprintf((char *)newval, " ++bin");
21377 else if (eap->force_bin == FORCE_NOBIN)
21378 sprintf((char *)newval, " ++nobin");
21379 else
21380 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021381
21382 if (eap->read_edit)
21383 STRCAT(newval, " ++edit");
21384
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021385 if (eap->force_ff != 0)
21386 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21387 eap->cmd + eap->force_ff);
21388# ifdef FEAT_MBYTE
21389 if (eap->force_enc != 0)
21390 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21391 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021392 if (eap->bad_char == BAD_KEEP)
21393 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21394 else if (eap->bad_char == BAD_DROP)
21395 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21396 else if (eap->bad_char != 0)
21397 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021398# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021399 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021400 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401}
21402#endif
21403
21404/*
21405 * Get the value of internal variable "name".
21406 * Return OK or FAIL.
21407 */
21408 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021409get_var_tv(
21410 char_u *name,
21411 int len, /* length of "name" */
21412 typval_T *rettv, /* NULL when only checking existence */
21413 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21414 int verbose, /* may give error message */
21415 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416{
21417 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021418 typval_T *tv = NULL;
21419 typval_T atv;
21420 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422
21423 /* truncate the name, so that we can use strcmp() */
21424 cc = name[len];
21425 name[len] = NUL;
21426
21427 /*
21428 * Check for "b:changedtick".
21429 */
21430 if (STRCMP(name, "b:changedtick") == 0)
21431 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021432 atv.v_type = VAR_NUMBER;
21433 atv.vval.v_number = curbuf->b_changedtick;
21434 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 }
21436
21437 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 * Check for user-defined variables.
21439 */
21440 else
21441 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021442 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021444 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021445 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021446 if (dip != NULL)
21447 *dip = v;
21448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 }
21450
Bram Moolenaare9a41262005-01-15 22:18:47 +000021451 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021453 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021454 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 ret = FAIL;
21456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021457 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021458 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459
21460 name[len] = cc;
21461
21462 return ret;
21463}
21464
21465/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021466 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21467 * Also handle function call with Funcref variable: func(expr)
21468 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21469 */
21470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021471handle_subscript(
21472 char_u **arg,
21473 typval_T *rettv,
21474 int evaluate, /* do more than finding the end */
21475 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021476{
21477 int ret = OK;
21478 dict_T *selfdict = NULL;
21479 char_u *s;
21480 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021481 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021482
21483 while (ret == OK
21484 && (**arg == '['
21485 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021486 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021487 && !vim_iswhite(*(*arg - 1)))
21488 {
21489 if (**arg == '(')
21490 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021491 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021492 if (evaluate)
21493 {
21494 functv = *rettv;
21495 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021496
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021497 /* Invoke the function. Recursive! */
21498 s = functv.vval.v_string;
21499 }
21500 else
21501 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021502 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021503 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21504 &len, evaluate, selfdict);
21505
21506 /* Clear the funcref afterwards, so that deleting it while
21507 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021508 if (evaluate)
21509 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021510
21511 /* Stop the expression evaluation when immediately aborting on
21512 * error, or when an interrupt occurred or an exception was thrown
21513 * but not caught. */
21514 if (aborting())
21515 {
21516 if (ret == OK)
21517 clear_tv(rettv);
21518 ret = FAIL;
21519 }
21520 dict_unref(selfdict);
21521 selfdict = NULL;
21522 }
21523 else /* **arg == '[' || **arg == '.' */
21524 {
21525 dict_unref(selfdict);
21526 if (rettv->v_type == VAR_DICT)
21527 {
21528 selfdict = rettv->vval.v_dict;
21529 if (selfdict != NULL)
21530 ++selfdict->dv_refcount;
21531 }
21532 else
21533 selfdict = NULL;
21534 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21535 {
21536 clear_tv(rettv);
21537 ret = FAIL;
21538 }
21539 }
21540 }
21541 dict_unref(selfdict);
21542 return ret;
21543}
21544
21545/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021546 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021547 * value).
21548 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021549 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021550alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021551{
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021553}
21554
21555/*
21556 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 * The string "s" must have been allocated, it is consumed.
21558 * Return NULL for out of memory, the variable otherwise.
21559 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021560 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021561alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562{
Bram Moolenaar33570922005-01-25 22:26:29 +000021563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021565 rettv = alloc_tv();
21566 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021568 rettv->v_type = VAR_STRING;
21569 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 }
21571 else
21572 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021573 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574}
21575
21576/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021577 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021580free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581{
21582 if (varp != NULL)
21583 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021584 switch (varp->v_type)
21585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021586 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021587 func_unref(varp->vval.v_string);
21588 /*FALLTHROUGH*/
21589 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590 vim_free(varp->vval.v_string);
21591 break;
21592 case VAR_LIST:
21593 list_unref(varp->vval.v_list);
21594 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021595 case VAR_DICT:
21596 dict_unref(varp->vval.v_dict);
21597 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021598 case VAR_JOB:
21599#ifdef FEAT_JOB
21600 job_unref(varp->vval.v_job);
21601 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021602#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021603 case VAR_CHANNEL:
21604#ifdef FEAT_CHANNEL
21605 channel_unref(varp->vval.v_channel);
21606 break;
21607#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021608 case VAR_NUMBER:
21609 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021610 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021611 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021612 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 vim_free(varp);
21615 }
21616}
21617
21618/*
21619 * Free the memory for a variable value and set the value to NULL or 0.
21620 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021622clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623{
21624 if (varp != NULL)
21625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021626 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021628 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021629 func_unref(varp->vval.v_string);
21630 /*FALLTHROUGH*/
21631 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021632 vim_free(varp->vval.v_string);
21633 varp->vval.v_string = NULL;
21634 break;
21635 case VAR_LIST:
21636 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021637 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021638 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021639 case VAR_DICT:
21640 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021641 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021642 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021643 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021644 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021645 varp->vval.v_number = 0;
21646 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021647 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021648#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021649 varp->vval.v_float = 0.0;
21650 break;
21651#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021652 case VAR_JOB:
21653#ifdef FEAT_JOB
21654 job_unref(varp->vval.v_job);
21655 varp->vval.v_job = NULL;
21656#endif
21657 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021658 case VAR_CHANNEL:
21659#ifdef FEAT_CHANNEL
21660 channel_unref(varp->vval.v_channel);
21661 varp->vval.v_channel = NULL;
21662#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021663 case VAR_UNKNOWN:
21664 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021666 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 }
21668}
21669
21670/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021671 * Set the value of a variable to NULL without freeing items.
21672 */
21673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021674init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021675{
21676 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021677 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021678}
21679
21680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 * Get the number value of a variable.
21682 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021683 * For incompatible types, return 0.
21684 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21685 * caller of incompatible types: it sets *denote to TRUE if "denote"
21686 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 */
21688 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021689get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021691 int error = FALSE;
21692
21693 return get_tv_number_chk(varp, &error); /* return 0L on error */
21694}
21695
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021696 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021697get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021698{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021699 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021701 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021703 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021704 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021705 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021706#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021707 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021708 break;
21709#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021710 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021711 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021712 break;
21713 case VAR_STRING:
21714 if (varp->vval.v_string != NULL)
21715 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021716 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021717 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021718 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021719 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021720 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021721 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021722 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021723 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021724 case VAR_SPECIAL:
21725 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21726 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021727 case VAR_JOB:
21728#ifdef FEAT_JOB
21729 EMSG(_("E910: Using a Job as a Number"));
21730 break;
21731#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021732 case VAR_CHANNEL:
21733#ifdef FEAT_CHANNEL
21734 EMSG(_("E913: Using a Channel as a Number"));
21735 break;
21736#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021737 case VAR_UNKNOWN:
21738 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021739 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021741 if (denote == NULL) /* useful for values that must be unsigned */
21742 n = -1;
21743 else
21744 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021745 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746}
21747
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021748#ifdef FEAT_FLOAT
21749 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021750get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021751{
21752 switch (varp->v_type)
21753 {
21754 case VAR_NUMBER:
21755 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021756 case VAR_FLOAT:
21757 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021758 case VAR_FUNC:
21759 EMSG(_("E891: Using a Funcref as a Float"));
21760 break;
21761 case VAR_STRING:
21762 EMSG(_("E892: Using a String as a Float"));
21763 break;
21764 case VAR_LIST:
21765 EMSG(_("E893: Using a List as a Float"));
21766 break;
21767 case VAR_DICT:
21768 EMSG(_("E894: Using a Dictionary as a Float"));
21769 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021770 case VAR_SPECIAL:
21771 EMSG(_("E907: Using a special value as a Float"));
21772 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021773 case VAR_JOB:
21774# ifdef FEAT_JOB
21775 EMSG(_("E911: Using a Job as a Float"));
21776 break;
21777# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021778 case VAR_CHANNEL:
21779# ifdef FEAT_CHANNEL
21780 EMSG(_("E914: Using a Channel as a Float"));
21781 break;
21782# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021783 case VAR_UNKNOWN:
21784 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021785 break;
21786 }
21787 return 0;
21788}
21789#endif
21790
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021792 * Get the lnum from the first argument.
21793 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021794 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 */
21796 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021797get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798{
Bram Moolenaar33570922005-01-25 22:26:29 +000021799 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 linenr_T lnum;
21801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021802 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 if (lnum == 0) /* no valid number, try using line() */
21804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021805 rettv.v_type = VAR_NUMBER;
21806 f_line(argvars, &rettv);
21807 lnum = rettv.vval.v_number;
21808 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 }
21810 return lnum;
21811}
21812
21813/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021814 * Get the lnum from the first argument.
21815 * Also accepts "$", then "buf" is used.
21816 * Returns 0 on error.
21817 */
21818 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021819get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021820{
21821 if (argvars[0].v_type == VAR_STRING
21822 && argvars[0].vval.v_string != NULL
21823 && argvars[0].vval.v_string[0] == '$'
21824 && buf != NULL)
21825 return buf->b_ml.ml_line_count;
21826 return get_tv_number_chk(&argvars[0], NULL);
21827}
21828
21829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 * Get the string value of a variable.
21831 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021832 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21833 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834 * If the String variable has never been set, return an empty string.
21835 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021836 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21837 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 */
21839 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021840get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841{
21842 static char_u mybuf[NUMBUFLEN];
21843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021844 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845}
21846
21847 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021848get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021850 char_u *res = get_tv_string_buf_chk(varp, buf);
21851
21852 return res != NULL ? res : (char_u *)"";
21853}
21854
Bram Moolenaar7d647822014-04-05 21:28:56 +020021855/*
21856 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21857 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021858 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021859get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021860{
21861 static char_u mybuf[NUMBUFLEN];
21862
21863 return get_tv_string_buf_chk(varp, mybuf);
21864}
21865
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021866 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021867get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021868{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021869 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021871 case VAR_NUMBER:
21872 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21873 return buf;
21874 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021875 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021876 break;
21877 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021878 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021879 break;
21880 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021881 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021882 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021883 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021884#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021885 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021886 break;
21887#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021888 case VAR_STRING:
21889 if (varp->vval.v_string != NULL)
21890 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021891 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021892 case VAR_SPECIAL:
21893 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21894 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021895 case VAR_JOB:
21896#ifdef FEAT_JOB
21897 {
21898 job_T *job = varp->vval.v_job;
21899 char *status = job->jv_status == JOB_FAILED ? "fail"
21900 : job->jv_status == JOB_ENDED ? "dead"
21901 : "run";
21902# ifdef UNIX
21903 vim_snprintf((char *)buf, NUMBUFLEN,
21904 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021905# elif defined(WIN32)
21906 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021907 "process %ld %s",
21908 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021909 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021910# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021911 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021912 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21913# endif
21914 return buf;
21915 }
21916#endif
21917 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021918 case VAR_CHANNEL:
21919#ifdef FEAT_CHANNEL
21920 {
21921 channel_T *channel = varp->vval.v_channel;
21922 char *status = channel_status(channel);
21923
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021924 if (channel == NULL)
21925 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21926 else
21927 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021928 "channel %d %s", channel->ch_id, status);
21929 return buf;
21930 }
21931#endif
21932 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021933 case VAR_UNKNOWN:
21934 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021935 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021937 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938}
21939
21940/*
21941 * Find variable "name" in the list of variables.
21942 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021943 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021944 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021945 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021947 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021948find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952
Bram Moolenaara7043832005-01-21 11:56:39 +000021953 ht = find_var_ht(name, &varname);
21954 if (htp != NULL)
21955 *htp = ht;
21956 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021958 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959}
21960
21961/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021962 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021963 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021965 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021966find_var_in_ht(
21967 hashtab_T *ht,
21968 int htname,
21969 char_u *varname,
21970 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021971{
Bram Moolenaar33570922005-01-25 22:26:29 +000021972 hashitem_T *hi;
21973
21974 if (*varname == NUL)
21975 {
21976 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021977 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021978 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021979 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021980 case 'g': return &globvars_var;
21981 case 'v': return &vimvars_var;
21982 case 'b': return &curbuf->b_bufvar;
21983 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021984#ifdef FEAT_WINDOWS
21985 case 't': return &curtab->tp_winvar;
21986#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021987 case 'l': return current_funccal == NULL
21988 ? NULL : &current_funccal->l_vars_var;
21989 case 'a': return current_funccal == NULL
21990 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021991 }
21992 return NULL;
21993 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021994
21995 hi = hash_find(ht, varname);
21996 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021997 {
21998 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021999 * worked find the variable again. Don't auto-load a script if it was
22000 * loaded already, otherwise it would be loaded every time when
22001 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022002 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022003 {
22004 /* Note: script_autoload() may make "hi" invalid. It must either
22005 * be obtained again or not used. */
22006 if (!script_autoload(varname, FALSE) || aborting())
22007 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022008 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022009 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022010 if (HASHITEM_EMPTY(hi))
22011 return NULL;
22012 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022013 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022014}
22015
22016/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022017 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022018 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022019 * Set "varname" to the start of name without ':'.
22020 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022021 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022022find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022024 hashitem_T *hi;
22025
Bram Moolenaar73627d02015-08-11 15:46:09 +020022026 if (name[0] == NUL)
22027 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 if (name[1] != ':')
22029 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022030 /* The name must not start with a colon or #. */
22031 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 return NULL;
22033 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022034
22035 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022036 hi = hash_find(&compat_hashtab, name);
22037 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022038 return &compat_hashtab;
22039
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022041 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022042 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 }
22044 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022045 if (*name == 'g') /* global variable */
22046 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022047 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22048 */
22049 if (vim_strchr(name + 2, ':') != NULL
22050 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022051 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022053 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022055 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022056#ifdef FEAT_WINDOWS
22057 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022058 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022059#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022060 if (*name == 'v') /* v: variable */
22061 return &vimvarht;
22062 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022063 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022064 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022065 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 if (*name == 's' /* script variable */
22067 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22068 return &SCRIPT_VARS(current_SID);
22069 return NULL;
22070}
22071
22072/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022073 * Get function call environment based on bactrace debug level
22074 */
22075 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022076get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022077{
22078 int i;
22079 funccall_T *funccal;
22080 funccall_T *temp_funccal;
22081
22082 funccal = current_funccal;
22083 if (debug_backtrace_level > 0)
22084 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022085 for (i = 0; i < debug_backtrace_level; i++)
22086 {
22087 temp_funccal = funccal->caller;
22088 if (temp_funccal)
22089 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022090 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022091 /* backtrace level overflow. reset to max */
22092 debug_backtrace_level = i;
22093 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022094 }
22095 return funccal;
22096}
22097
22098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022100 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101 * Returns NULL when it doesn't exist.
22102 */
22103 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022104get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105{
Bram Moolenaar33570922005-01-25 22:26:29 +000022106 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022108 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109 if (v == NULL)
22110 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022111 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112}
22113
22114/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022115 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116 * sourcing this script and when executing functions defined in the script.
22117 */
22118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022119new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120{
Bram Moolenaara7043832005-01-21 11:56:39 +000022121 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022122 hashtab_T *ht;
22123 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022124
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22126 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022127 /* Re-allocating ga_data means that an ht_array pointing to
22128 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022129 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022130 for (i = 1; i <= ga_scripts.ga_len; ++i)
22131 {
22132 ht = &SCRIPT_VARS(i);
22133 if (ht->ht_mask == HT_INIT_SIZE - 1)
22134 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022135 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022136 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022137 }
22138
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 while (ga_scripts.ga_len < id)
22140 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022141 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022142 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022143 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 }
22146 }
22147}
22148
22149/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022150 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22151 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 */
22153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022154init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155{
Bram Moolenaar33570922005-01-25 22:26:29 +000022156 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022157 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022158 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022159 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022160 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022161 dict_var->di_tv.vval.v_dict = dict;
22162 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022163 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022164 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22165 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166}
22167
22168/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022169 * Unreference a dictionary initialized by init_var_dict().
22170 */
22171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022172unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022173{
22174 /* Now the dict needs to be freed if no one else is using it, go back to
22175 * normal reference counting. */
22176 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22177 dict_unref(dict);
22178}
22179
22180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 * Frees all allocated variables and the value they contain.
22183 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 */
22185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022186vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022187{
22188 vars_clear_ext(ht, TRUE);
22189}
22190
22191/*
22192 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22193 */
22194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022195vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196{
Bram Moolenaara7043832005-01-21 11:56:39 +000022197 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022198 hashitem_T *hi;
22199 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200
Bram Moolenaar33570922005-01-25 22:26:29 +000022201 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022202 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022203 for (hi = ht->ht_array; todo > 0; ++hi)
22204 {
22205 if (!HASHITEM_EMPTY(hi))
22206 {
22207 --todo;
22208
Bram Moolenaar33570922005-01-25 22:26:29 +000022209 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022210 * ht_array might change then. hash_clear() takes care of it
22211 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022212 v = HI2DI(hi);
22213 if (free_val)
22214 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022215 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022216 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022217 }
22218 }
22219 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022220 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221}
22222
Bram Moolenaara7043832005-01-21 11:56:39 +000022223/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022224 * Delete a variable from hashtab "ht" at item "hi".
22225 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022228delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229{
Bram Moolenaar33570922005-01-25 22:26:29 +000022230 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022231
22232 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022233 clear_tv(&di->di_tv);
22234 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235}
22236
22237/*
22238 * List the value of one internal variable.
22239 */
22240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022241list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022243 char_u *tofree;
22244 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022245 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022246
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022247 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022248 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022249 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022250 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251}
22252
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022254list_one_var_a(
22255 char_u *prefix,
22256 char_u *name,
22257 int type,
22258 char_u *string,
22259 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260{
Bram Moolenaar31859182007-08-14 20:41:13 +000022261 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22262 msg_start();
22263 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 if (name != NULL) /* "a:" vars don't have a name stored */
22265 msg_puts(name);
22266 msg_putchar(' ');
22267 msg_advance(22);
22268 if (type == VAR_NUMBER)
22269 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022270 else if (type == VAR_FUNC)
22271 msg_putchar('*');
22272 else if (type == VAR_LIST)
22273 {
22274 msg_putchar('[');
22275 if (*string == '[')
22276 ++string;
22277 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022278 else if (type == VAR_DICT)
22279 {
22280 msg_putchar('{');
22281 if (*string == '{')
22282 ++string;
22283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 else
22285 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022286
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022288
22289 if (type == VAR_FUNC)
22290 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022291 if (*first)
22292 {
22293 msg_clr_eos();
22294 *first = FALSE;
22295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296}
22297
22298/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022299 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300 * If the variable already exists, the value is updated.
22301 * Otherwise the variable is created.
22302 */
22303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022304set_var(
22305 char_u *name,
22306 typval_T *tv,
22307 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308{
Bram Moolenaar33570922005-01-25 22:26:29 +000022309 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022311 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022312
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022313 ht = find_var_ht(name, &varname);
22314 if (ht == NULL || *varname == NUL)
22315 {
22316 EMSG2(_(e_illvar), name);
22317 return;
22318 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022319 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022320
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022321 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22322 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022323
Bram Moolenaar33570922005-01-25 22:26:29 +000022324 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022326 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022327 if (var_check_ro(v->di_flags, name, FALSE)
22328 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022329 return;
22330 if (v->di_tv.v_type != tv->v_type
22331 && !((v->di_tv.v_type == VAR_STRING
22332 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022333 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022334 || tv->v_type == VAR_NUMBER))
22335#ifdef FEAT_FLOAT
22336 && !((v->di_tv.v_type == VAR_NUMBER
22337 || v->di_tv.v_type == VAR_FLOAT)
22338 && (tv->v_type == VAR_NUMBER
22339 || tv->v_type == VAR_FLOAT))
22340#endif
22341 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022342 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022343 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022344 return;
22345 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022346
22347 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022348 * Handle setting internal v: variables separately where needed to
22349 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022350 */
22351 if (ht == &vimvarht)
22352 {
22353 if (v->di_tv.v_type == VAR_STRING)
22354 {
22355 vim_free(v->di_tv.vval.v_string);
22356 if (copy || tv->v_type != VAR_STRING)
22357 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22358 else
22359 {
22360 /* Take over the string to avoid an extra alloc/free. */
22361 v->di_tv.vval.v_string = tv->vval.v_string;
22362 tv->vval.v_string = NULL;
22363 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022364 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022365 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022366 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022367 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022368 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022369 if (STRCMP(varname, "searchforward") == 0)
22370 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022371#ifdef FEAT_SEARCH_EXTRA
22372 else if (STRCMP(varname, "hlsearch") == 0)
22373 {
22374 no_hlsearch = !v->di_tv.vval.v_number;
22375 redraw_all_later(SOME_VALID);
22376 }
22377#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022378 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022379 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022380 else if (v->di_tv.v_type != tv->v_type)
22381 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022382 }
22383
22384 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 }
22386 else /* add a new variable */
22387 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022388 /* Can't add "v:" variable. */
22389 if (ht == &vimvarht)
22390 {
22391 EMSG2(_(e_illvar), name);
22392 return;
22393 }
22394
Bram Moolenaar92124a32005-06-17 22:03:40 +000022395 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022396 if (!valid_varname(varname))
22397 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022398
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022399 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22400 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022401 if (v == NULL)
22402 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022403 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022404 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022406 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 return;
22408 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022409 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022411
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022412 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022413 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022414 else
22415 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022416 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022417 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022418 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420}
22421
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022422/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022423 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022424 * Also give an error message.
22425 */
22426 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022427var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022428{
22429 if (flags & DI_FLAGS_RO)
22430 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022431 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022432 return TRUE;
22433 }
22434 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22435 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022436 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022437 return TRUE;
22438 }
22439 return FALSE;
22440}
22441
22442/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022443 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22444 * Also give an error message.
22445 */
22446 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022447var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022448{
22449 if (flags & DI_FLAGS_FIX)
22450 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022451 EMSG2(_("E795: Cannot delete variable %s"),
22452 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022453 return TRUE;
22454 }
22455 return FALSE;
22456}
22457
22458/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022459 * Check if a funcref is assigned to a valid variable name.
22460 * Return TRUE and give an error if not.
22461 */
22462 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022463var_check_func_name(
22464 char_u *name, /* points to start of variable name */
22465 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022466{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022467 /* Allow for w: b: s: and t:. */
22468 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022469 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22470 ? name[2] : name[0]))
22471 {
22472 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22473 name);
22474 return TRUE;
22475 }
22476 /* Don't allow hiding a function. When "v" is not NULL we might be
22477 * assigning another function to the same var, the type is checked
22478 * below. */
22479 if (new_var && function_exists(name))
22480 {
22481 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22482 name);
22483 return TRUE;
22484 }
22485 return FALSE;
22486}
22487
22488/*
22489 * Check if a variable name is valid.
22490 * Return FALSE and give an error if not.
22491 */
22492 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022493valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022494{
22495 char_u *p;
22496
22497 for (p = varname; *p != NUL; ++p)
22498 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22499 && *p != AUTOLOAD_CHAR)
22500 {
22501 EMSG2(_(e_illvar), varname);
22502 return FALSE;
22503 }
22504 return TRUE;
22505}
22506
22507/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022508 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022509 * Also give an error message, using "name" or _("name") when use_gettext is
22510 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022511 */
22512 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022513tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022514{
22515 if (lock & VAR_LOCKED)
22516 {
22517 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022518 name == NULL ? (char_u *)_("Unknown")
22519 : use_gettext ? (char_u *)_(name)
22520 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022521 return TRUE;
22522 }
22523 if (lock & VAR_FIXED)
22524 {
22525 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022526 name == NULL ? (char_u *)_("Unknown")
22527 : use_gettext ? (char_u *)_(name)
22528 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022529 return TRUE;
22530 }
22531 return FALSE;
22532}
22533
22534/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022535 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022536 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022537 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022538 * It is OK for "from" and "to" to point to the same item. This is used to
22539 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022540 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022541 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022542copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022544 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022545 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022546 switch (from->v_type)
22547 {
22548 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022549 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022550 to->vval.v_number = from->vval.v_number;
22551 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022552 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022553#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022554 to->vval.v_float = from->vval.v_float;
22555 break;
22556#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022557 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022558#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022559 to->vval.v_job = from->vval.v_job;
22560 ++to->vval.v_job->jv_refcount;
22561 break;
22562#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022563 case VAR_CHANNEL:
22564#ifdef FEAT_CHANNEL
22565 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022566 if (to->vval.v_channel != NULL)
22567 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022568 break;
22569#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022570 case VAR_STRING:
22571 case VAR_FUNC:
22572 if (from->vval.v_string == NULL)
22573 to->vval.v_string = NULL;
22574 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022575 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022576 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022577 if (from->v_type == VAR_FUNC)
22578 func_ref(to->vval.v_string);
22579 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022580 break;
22581 case VAR_LIST:
22582 if (from->vval.v_list == NULL)
22583 to->vval.v_list = NULL;
22584 else
22585 {
22586 to->vval.v_list = from->vval.v_list;
22587 ++to->vval.v_list->lv_refcount;
22588 }
22589 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022590 case VAR_DICT:
22591 if (from->vval.v_dict == NULL)
22592 to->vval.v_dict = NULL;
22593 else
22594 {
22595 to->vval.v_dict = from->vval.v_dict;
22596 ++to->vval.v_dict->dv_refcount;
22597 }
22598 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022599 case VAR_UNKNOWN:
22600 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022601 break;
22602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603}
22604
22605/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022606 * Make a copy of an item.
22607 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022608 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22609 * reference to an already copied list/dict can be used.
22610 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022611 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022613item_copy(
22614 typval_T *from,
22615 typval_T *to,
22616 int deep,
22617 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022618{
22619 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022620 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022621
Bram Moolenaar33570922005-01-25 22:26:29 +000022622 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022623 {
22624 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022625 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022626 }
22627 ++recurse;
22628
22629 switch (from->v_type)
22630 {
22631 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022632 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022633 case VAR_STRING:
22634 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022635 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022636 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022637 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022638 copy_tv(from, to);
22639 break;
22640 case VAR_LIST:
22641 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022642 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022643 if (from->vval.v_list == NULL)
22644 to->vval.v_list = NULL;
22645 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22646 {
22647 /* use the copy made earlier */
22648 to->vval.v_list = from->vval.v_list->lv_copylist;
22649 ++to->vval.v_list->lv_refcount;
22650 }
22651 else
22652 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22653 if (to->vval.v_list == NULL)
22654 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022655 break;
22656 case VAR_DICT:
22657 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022658 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022659 if (from->vval.v_dict == NULL)
22660 to->vval.v_dict = NULL;
22661 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22662 {
22663 /* use the copy made earlier */
22664 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22665 ++to->vval.v_dict->dv_refcount;
22666 }
22667 else
22668 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22669 if (to->vval.v_dict == NULL)
22670 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022671 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022672 case VAR_UNKNOWN:
22673 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022674 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022675 }
22676 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022677 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022678}
22679
22680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 * ":echo expr1 ..." print each argument separated with a space, add a
22682 * newline at the end.
22683 * ":echon expr1 ..." print each argument plain.
22684 */
22685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022686ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687{
22688 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022689 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022690 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 char_u *p;
22692 int needclr = TRUE;
22693 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022694 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695
22696 if (eap->skip)
22697 ++emsg_skip;
22698 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22699 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022700 /* If eval1() causes an error message the text from the command may
22701 * still need to be cleared. E.g., "echo 22,44". */
22702 need_clr_eos = needclr;
22703
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022705 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 {
22707 /*
22708 * Report the invalid expression unless the expression evaluation
22709 * has been cancelled due to an aborting error, an interrupt, or an
22710 * exception.
22711 */
22712 if (!aborting())
22713 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022714 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 break;
22716 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022717 need_clr_eos = FALSE;
22718
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 if (!eap->skip)
22720 {
22721 if (atstart)
22722 {
22723 atstart = FALSE;
22724 /* Call msg_start() after eval1(), evaluating the expression
22725 * may cause a message to appear. */
22726 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022727 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022728 /* Mark the saved text as finishing the line, so that what
22729 * follows is displayed on a new line when scrolling back
22730 * at the more prompt. */
22731 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 }
22735 else if (eap->cmdidx == CMD_echo)
22736 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022737 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022738 if (p != NULL)
22739 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022741 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022743 if (*p != TAB && needclr)
22744 {
22745 /* remove any text still there from the command */
22746 msg_clr_eos();
22747 needclr = FALSE;
22748 }
22749 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 }
22751 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022752 {
22753#ifdef FEAT_MBYTE
22754 if (has_mbyte)
22755 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022756 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022757
22758 (void)msg_outtrans_len_attr(p, i, echo_attr);
22759 p += i - 1;
22760 }
22761 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022763 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022766 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022768 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 arg = skipwhite(arg);
22770 }
22771 eap->nextcmd = check_nextcmd(arg);
22772
22773 if (eap->skip)
22774 --emsg_skip;
22775 else
22776 {
22777 /* remove text that may still be there from the command */
22778 if (needclr)
22779 msg_clr_eos();
22780 if (eap->cmdidx == CMD_echo)
22781 msg_end();
22782 }
22783}
22784
22785/*
22786 * ":echohl {name}".
22787 */
22788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022789ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790{
22791 int id;
22792
22793 id = syn_name2id(eap->arg);
22794 if (id == 0)
22795 echo_attr = 0;
22796 else
22797 echo_attr = syn_id2attr(id);
22798}
22799
22800/*
22801 * ":execute expr1 ..." execute the result of an expression.
22802 * ":echomsg expr1 ..." Print a message
22803 * ":echoerr expr1 ..." Print an error
22804 * Each gets spaces around each argument and a newline at the end for
22805 * echo commands
22806 */
22807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022808ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809{
22810 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022811 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 int ret = OK;
22813 char_u *p;
22814 garray_T ga;
22815 int len;
22816 int save_did_emsg;
22817
22818 ga_init2(&ga, 1, 80);
22819
22820 if (eap->skip)
22821 ++emsg_skip;
22822 while (*arg != NUL && *arg != '|' && *arg != '\n')
22823 {
22824 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022825 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 {
22827 /*
22828 * Report the invalid expression unless the expression evaluation
22829 * has been cancelled due to an aborting error, an interrupt, or an
22830 * exception.
22831 */
22832 if (!aborting())
22833 EMSG2(_(e_invexpr2), p);
22834 ret = FAIL;
22835 break;
22836 }
22837
22838 if (!eap->skip)
22839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022840 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 len = (int)STRLEN(p);
22842 if (ga_grow(&ga, len + 2) == FAIL)
22843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022844 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 ret = FAIL;
22846 break;
22847 }
22848 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 ga.ga_len += len;
22852 }
22853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022854 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 arg = skipwhite(arg);
22856 }
22857
22858 if (ret != FAIL && ga.ga_data != NULL)
22859 {
22860 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022863 out_flush();
22864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 else if (eap->cmdidx == CMD_echoerr)
22866 {
22867 /* We don't want to abort following commands, restore did_emsg. */
22868 save_did_emsg = did_emsg;
22869 EMSG((char_u *)ga.ga_data);
22870 if (!force_abort)
22871 did_emsg = save_did_emsg;
22872 }
22873 else if (eap->cmdidx == CMD_execute)
22874 do_cmdline((char_u *)ga.ga_data,
22875 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22876 }
22877
22878 ga_clear(&ga);
22879
22880 if (eap->skip)
22881 --emsg_skip;
22882
22883 eap->nextcmd = check_nextcmd(arg);
22884}
22885
22886/*
22887 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22888 * "arg" points to the "&" or '+' when called, to "option" when returning.
22889 * Returns NULL when no option name found. Otherwise pointer to the char
22890 * after the option name.
22891 */
22892 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022893find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894{
22895 char_u *p = *arg;
22896
22897 ++p;
22898 if (*p == 'g' && p[1] == ':')
22899 {
22900 *opt_flags = OPT_GLOBAL;
22901 p += 2;
22902 }
22903 else if (*p == 'l' && p[1] == ':')
22904 {
22905 *opt_flags = OPT_LOCAL;
22906 p += 2;
22907 }
22908 else
22909 *opt_flags = 0;
22910
22911 if (!ASCII_ISALPHA(*p))
22912 return NULL;
22913 *arg = p;
22914
22915 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22916 p += 4; /* termcap option */
22917 else
22918 while (ASCII_ISALPHA(*p))
22919 ++p;
22920 return p;
22921}
22922
22923/*
22924 * ":function"
22925 */
22926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022927ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928{
22929 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022930 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 int j;
22932 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022934 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 char_u *name = NULL;
22936 char_u *p;
22937 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022938 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 garray_T newargs;
22940 garray_T newlines;
22941 int varargs = FALSE;
22942 int mustend = FALSE;
22943 int flags = 0;
22944 ufunc_T *fp;
22945 int indent;
22946 int nesting;
22947 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022948 dictitem_T *v;
22949 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022950 static int func_nr = 0; /* number for nameless function */
22951 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022952 hashtab_T *ht;
22953 int todo;
22954 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022955 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956
22957 /*
22958 * ":function" without argument: list functions.
22959 */
22960 if (ends_excmd(*eap->arg))
22961 {
22962 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022963 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022964 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022965 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022966 {
22967 if (!HASHITEM_EMPTY(hi))
22968 {
22969 --todo;
22970 fp = HI2UF(hi);
22971 if (!isdigit(*fp->uf_name))
22972 list_func_head(fp, FALSE);
22973 }
22974 }
22975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 eap->nextcmd = check_nextcmd(eap->arg);
22977 return;
22978 }
22979
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022980 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022981 * ":function /pat": list functions matching pattern.
22982 */
22983 if (*eap->arg == '/')
22984 {
22985 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22986 if (!eap->skip)
22987 {
22988 regmatch_T regmatch;
22989
22990 c = *p;
22991 *p = NUL;
22992 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22993 *p = c;
22994 if (regmatch.regprog != NULL)
22995 {
22996 regmatch.rm_ic = p_ic;
22997
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022998 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022999 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23000 {
23001 if (!HASHITEM_EMPTY(hi))
23002 {
23003 --todo;
23004 fp = HI2UF(hi);
23005 if (!isdigit(*fp->uf_name)
23006 && vim_regexec(&regmatch, fp->uf_name, 0))
23007 list_func_head(fp, FALSE);
23008 }
23009 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023010 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023011 }
23012 }
23013 if (*p == '/')
23014 ++p;
23015 eap->nextcmd = check_nextcmd(p);
23016 return;
23017 }
23018
23019 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023020 * Get the function name. There are these situations:
23021 * func normal function name
23022 * "name" == func, "fudi.fd_dict" == NULL
23023 * dict.func new dictionary entry
23024 * "name" == NULL, "fudi.fd_dict" set,
23025 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23026 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023027 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023028 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23029 * dict.func existing dict entry that's not a Funcref
23030 * "name" == NULL, "fudi.fd_dict" set,
23031 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023032 * s:func script-local function name
23033 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023036 name = trans_function_name(&p, eap->skip, 0, &fudi);
23037 paren = (vim_strchr(p, '(') != NULL);
23038 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039 {
23040 /*
23041 * Return on an invalid expression in braces, unless the expression
23042 * evaluation has been cancelled due to an aborting error, an
23043 * interrupt, or an exception.
23044 */
23045 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023046 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023047 if (!eap->skip && fudi.fd_newkey != NULL)
23048 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023049 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 else
23053 eap->skip = TRUE;
23054 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023055
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 /* An error in a function call during evaluation of an expression in magic
23057 * braces should not cause the function not to be defined. */
23058 saved_did_emsg = did_emsg;
23059 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060
23061 /*
23062 * ":function func" with only function name: list function.
23063 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023064 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 {
23066 if (!ends_excmd(*skipwhite(p)))
23067 {
23068 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023069 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 }
23071 eap->nextcmd = check_nextcmd(p);
23072 if (eap->nextcmd != NULL)
23073 *p = NUL;
23074 if (!eap->skip && !got_int)
23075 {
23076 fp = find_func(name);
23077 if (fp != NULL)
23078 {
23079 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023080 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023082 if (FUNCLINE(fp, j) == NULL)
23083 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 msg_putchar('\n');
23085 msg_outnum((long)(j + 1));
23086 if (j < 9)
23087 msg_putchar(' ');
23088 if (j < 99)
23089 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023090 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 out_flush(); /* show a line at a time */
23092 ui_breakcheck();
23093 }
23094 if (!got_int)
23095 {
23096 msg_putchar('\n');
23097 msg_puts((char_u *)" endfunction");
23098 }
23099 }
23100 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023101 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023103 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 }
23105
23106 /*
23107 * ":function name(arg1, arg2)" Define function.
23108 */
23109 p = skipwhite(p);
23110 if (*p != '(')
23111 {
23112 if (!eap->skip)
23113 {
23114 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023115 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 }
23117 /* attempt to continue by skipping some text */
23118 if (vim_strchr(p, '(') != NULL)
23119 p = vim_strchr(p, '(');
23120 }
23121 p = skipwhite(p + 1);
23122
23123 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23124 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23125
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023126 if (!eap->skip)
23127 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023128 /* Check the name of the function. Unless it's a dictionary function
23129 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023130 if (name != NULL)
23131 arg = name;
23132 else
23133 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023134 if (arg != NULL && (fudi.fd_di == NULL
23135 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023136 {
23137 if (*arg == K_SPECIAL)
23138 j = 3;
23139 else
23140 j = 0;
23141 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23142 : eval_isnamec(arg[j])))
23143 ++j;
23144 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023145 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023146 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023147 /* Disallow using the g: dict. */
23148 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23149 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023150 }
23151
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 /*
23153 * Isolate the arguments: "arg1, arg2, ...)"
23154 */
23155 while (*p != ')')
23156 {
23157 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23158 {
23159 varargs = TRUE;
23160 p += 3;
23161 mustend = TRUE;
23162 }
23163 else
23164 {
23165 arg = p;
23166 while (ASCII_ISALNUM(*p) || *p == '_')
23167 ++p;
23168 if (arg == p || isdigit(*arg)
23169 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23170 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23171 {
23172 if (!eap->skip)
23173 EMSG2(_("E125: Illegal argument: %s"), arg);
23174 break;
23175 }
23176 if (ga_grow(&newargs, 1) == FAIL)
23177 goto erret;
23178 c = *p;
23179 *p = NUL;
23180 arg = vim_strsave(arg);
23181 if (arg == NULL)
23182 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023183
23184 /* Check for duplicate argument name. */
23185 for (i = 0; i < newargs.ga_len; ++i)
23186 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23187 {
23188 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023189 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023190 goto erret;
23191 }
23192
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23194 *p = c;
23195 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 if (*p == ',')
23197 ++p;
23198 else
23199 mustend = TRUE;
23200 }
23201 p = skipwhite(p);
23202 if (mustend && *p != ')')
23203 {
23204 if (!eap->skip)
23205 EMSG2(_(e_invarg2), eap->arg);
23206 break;
23207 }
23208 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023209 if (*p != ')')
23210 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 ++p; /* skip the ')' */
23212
Bram Moolenaare9a41262005-01-15 22:18:47 +000023213 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 for (;;)
23215 {
23216 p = skipwhite(p);
23217 if (STRNCMP(p, "range", 5) == 0)
23218 {
23219 flags |= FC_RANGE;
23220 p += 5;
23221 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023222 else if (STRNCMP(p, "dict", 4) == 0)
23223 {
23224 flags |= FC_DICT;
23225 p += 4;
23226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227 else if (STRNCMP(p, "abort", 5) == 0)
23228 {
23229 flags |= FC_ABORT;
23230 p += 5;
23231 }
23232 else
23233 break;
23234 }
23235
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023236 /* When there is a line break use what follows for the function body.
23237 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23238 if (*p == '\n')
23239 line_arg = p + 1;
23240 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 EMSG(_(e_trailing));
23242
23243 /*
23244 * Read the body of the function, until ":endfunction" is found.
23245 */
23246 if (KeyTyped)
23247 {
23248 /* Check if the function already exists, don't let the user type the
23249 * whole function before telling him it doesn't work! For a script we
23250 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023251 if (!eap->skip && !eap->forceit)
23252 {
23253 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23254 EMSG(_(e_funcdict));
23255 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023256 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023259 if (!eap->skip && did_emsg)
23260 goto erret;
23261
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262 msg_putchar('\n'); /* don't overwrite the function name */
23263 cmdline_row = msg_row;
23264 }
23265
23266 indent = 2;
23267 nesting = 0;
23268 for (;;)
23269 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023270 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023271 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023272 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023273 saved_wait_return = FALSE;
23274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023276 sourcing_lnum_off = sourcing_lnum;
23277
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023278 if (line_arg != NULL)
23279 {
23280 /* Use eap->arg, split up in parts by line breaks. */
23281 theline = line_arg;
23282 p = vim_strchr(theline, '\n');
23283 if (p == NULL)
23284 line_arg += STRLEN(line_arg);
23285 else
23286 {
23287 *p = NUL;
23288 line_arg = p + 1;
23289 }
23290 }
23291 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 theline = getcmdline(':', 0L, indent);
23293 else
23294 theline = eap->getline(':', eap->cookie, indent);
23295 if (KeyTyped)
23296 lines_left = Rows - 1;
23297 if (theline == NULL)
23298 {
23299 EMSG(_("E126: Missing :endfunction"));
23300 goto erret;
23301 }
23302
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023303 /* Detect line continuation: sourcing_lnum increased more than one. */
23304 if (sourcing_lnum > sourcing_lnum_off + 1)
23305 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23306 else
23307 sourcing_lnum_off = 0;
23308
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 if (skip_until != NULL)
23310 {
23311 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23312 * don't check for ":endfunc". */
23313 if (STRCMP(theline, skip_until) == 0)
23314 {
23315 vim_free(skip_until);
23316 skip_until = NULL;
23317 }
23318 }
23319 else
23320 {
23321 /* skip ':' and blanks*/
23322 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23323 ;
23324
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023325 /* Check for "endfunction". */
23326 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023328 if (line_arg == NULL)
23329 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 break;
23331 }
23332
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023333 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023334 * at "end". */
23335 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23336 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023337 else if (STRNCMP(p, "if", 2) == 0
23338 || STRNCMP(p, "wh", 2) == 0
23339 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 || STRNCMP(p, "try", 3) == 0)
23341 indent += 2;
23342
23343 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023344 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023346 if (*p == '!')
23347 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023349 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23350 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023352 ++nesting;
23353 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 }
23355 }
23356
23357 /* Check for ":append" or ":insert". */
23358 p = skip_range(p, NULL);
23359 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23360 || (p[0] == 'i'
23361 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23362 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23363 skip_until = vim_strsave((char_u *)".");
23364
23365 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23366 arg = skipwhite(skiptowhite(p));
23367 if (arg[0] == '<' && arg[1] =='<'
23368 && ((p[0] == 'p' && p[1] == 'y'
23369 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23370 || (p[0] == 'p' && p[1] == 'e'
23371 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23372 || (p[0] == 't' && p[1] == 'c'
23373 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023374 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23375 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23377 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023378 || (p[0] == 'm' && p[1] == 'z'
23379 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 ))
23381 {
23382 /* ":python <<" continues until a dot, like ":append" */
23383 p = skipwhite(arg + 2);
23384 if (*p == NUL)
23385 skip_until = vim_strsave((char_u *)".");
23386 else
23387 skip_until = vim_strsave(p);
23388 }
23389 }
23390
23391 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023392 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023393 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023394 if (line_arg == NULL)
23395 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023397 }
23398
23399 /* Copy the line to newly allocated memory. get_one_sourceline()
23400 * allocates 250 bytes per line, this saves 80% on average. The cost
23401 * is an extra alloc/free. */
23402 p = vim_strsave(theline);
23403 if (p != NULL)
23404 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023405 if (line_arg == NULL)
23406 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023407 theline = p;
23408 }
23409
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023410 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23411
23412 /* Add NULL lines for continuation lines, so that the line count is
23413 * equal to the index in the growarray. */
23414 while (sourcing_lnum_off-- > 0)
23415 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023416
23417 /* Check for end of eap->arg. */
23418 if (line_arg != NULL && *line_arg == NUL)
23419 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023420 }
23421
23422 /* Don't define the function when skipping commands or when an error was
23423 * detected. */
23424 if (eap->skip || did_emsg)
23425 goto erret;
23426
23427 /*
23428 * If there are no errors, add the function
23429 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023430 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023431 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023432 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023433 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023434 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023435 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023436 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023437 goto erret;
23438 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023439
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023440 fp = find_func(name);
23441 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023443 if (!eap->forceit)
23444 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023445 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023446 goto erret;
23447 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023448 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023449 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023450 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023451 name);
23452 goto erret;
23453 }
23454 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023455 ga_clear_strings(&(fp->uf_args));
23456 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023457 vim_free(name);
23458 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 }
23461 else
23462 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023463 char numbuf[20];
23464
23465 fp = NULL;
23466 if (fudi.fd_newkey == NULL && !eap->forceit)
23467 {
23468 EMSG(_(e_funcdict));
23469 goto erret;
23470 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023471 if (fudi.fd_di == NULL)
23472 {
23473 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023474 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023475 goto erret;
23476 }
23477 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023478 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023479 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023480
23481 /* Give the function a sequential number. Can only be used with a
23482 * Funcref! */
23483 vim_free(name);
23484 sprintf(numbuf, "%d", ++func_nr);
23485 name = vim_strsave((char_u *)numbuf);
23486 if (name == NULL)
23487 goto erret;
23488 }
23489
23490 if (fp == NULL)
23491 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023492 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023493 {
23494 int slen, plen;
23495 char_u *scriptname;
23496
23497 /* Check that the autoload name matches the script name. */
23498 j = FAIL;
23499 if (sourcing_name != NULL)
23500 {
23501 scriptname = autoload_name(name);
23502 if (scriptname != NULL)
23503 {
23504 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023505 plen = (int)STRLEN(p);
23506 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023507 if (slen > plen && fnamecmp(p,
23508 sourcing_name + slen - plen) == 0)
23509 j = OK;
23510 vim_free(scriptname);
23511 }
23512 }
23513 if (j == FAIL)
23514 {
23515 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23516 goto erret;
23517 }
23518 }
23519
23520 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 if (fp == NULL)
23522 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023523
23524 if (fudi.fd_dict != NULL)
23525 {
23526 if (fudi.fd_di == NULL)
23527 {
23528 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023529 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023530 if (fudi.fd_di == NULL)
23531 {
23532 vim_free(fp);
23533 goto erret;
23534 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023535 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23536 {
23537 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023538 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023539 goto erret;
23540 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023541 }
23542 else
23543 /* overwrite existing dict entry */
23544 clear_tv(&fudi.fd_di->di_tv);
23545 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023546 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023547 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023548 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023549
23550 /* behave like "dict" was used */
23551 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023552 }
23553
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023555 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023556 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23557 {
23558 vim_free(fp);
23559 goto erret;
23560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023562 fp->uf_args = newargs;
23563 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023564#ifdef FEAT_PROFILE
23565 fp->uf_tml_count = NULL;
23566 fp->uf_tml_total = NULL;
23567 fp->uf_tml_self = NULL;
23568 fp->uf_profiling = FALSE;
23569 if (prof_def_func())
23570 func_do_profile(fp);
23571#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023572 fp->uf_varargs = varargs;
23573 fp->uf_flags = flags;
23574 fp->uf_calls = 0;
23575 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023576 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577
23578erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579 ga_clear_strings(&newargs);
23580 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023581ret_free:
23582 vim_free(skip_until);
23583 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023586 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587}
23588
23589/*
23590 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023591 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023593 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023594 * TFN_INT: internal function name OK
23595 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023596 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 * Advances "pp" to just after the function name (if no error).
23598 */
23599 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023600trans_function_name(
23601 char_u **pp,
23602 int skip, /* only find the end, don't evaluate */
23603 int flags,
23604 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023606 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607 char_u *start;
23608 char_u *end;
23609 int lead;
23610 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023612 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023613
23614 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023615 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023617
23618 /* Check for hard coded <SNR>: already translated function ID (from a user
23619 * command). */
23620 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23621 && (*pp)[2] == (int)KE_SNR)
23622 {
23623 *pp += 3;
23624 len = get_id_len(pp) + 3;
23625 return vim_strnsave(start, len);
23626 }
23627
23628 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23629 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023631 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023633
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023634 /* Note that TFN_ flags use the same values as GLV_ flags. */
23635 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023636 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023637 if (end == start)
23638 {
23639 if (!skip)
23640 EMSG(_("E129: Function name required"));
23641 goto theend;
23642 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023643 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023644 {
23645 /*
23646 * Report an invalid expression in braces, unless the expression
23647 * evaluation has been cancelled due to an aborting error, an
23648 * interrupt, or an exception.
23649 */
23650 if (!aborting())
23651 {
23652 if (end != NULL)
23653 EMSG2(_(e_invarg2), start);
23654 }
23655 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023656 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023657 goto theend;
23658 }
23659
23660 if (lv.ll_tv != NULL)
23661 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023662 if (fdp != NULL)
23663 {
23664 fdp->fd_dict = lv.ll_dict;
23665 fdp->fd_newkey = lv.ll_newkey;
23666 lv.ll_newkey = NULL;
23667 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023668 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023669 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23670 {
23671 name = vim_strsave(lv.ll_tv->vval.v_string);
23672 *pp = end;
23673 }
23674 else
23675 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023676 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23677 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023678 EMSG(_(e_funcref));
23679 else
23680 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023681 name = NULL;
23682 }
23683 goto theend;
23684 }
23685
23686 if (lv.ll_name == NULL)
23687 {
23688 /* Error found, but continue after the function name. */
23689 *pp = end;
23690 goto theend;
23691 }
23692
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023693 /* Check if the name is a Funcref. If so, use the value. */
23694 if (lv.ll_exp_name != NULL)
23695 {
23696 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023697 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023698 if (name == lv.ll_exp_name)
23699 name = NULL;
23700 }
23701 else
23702 {
23703 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023704 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023705 if (name == *pp)
23706 name = NULL;
23707 }
23708 if (name != NULL)
23709 {
23710 name = vim_strsave(name);
23711 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023712 if (STRNCMP(name, "<SNR>", 5) == 0)
23713 {
23714 /* Change "<SNR>" to the byte sequence. */
23715 name[0] = K_SPECIAL;
23716 name[1] = KS_EXTRA;
23717 name[2] = (int)KE_SNR;
23718 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23719 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023720 goto theend;
23721 }
23722
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023723 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023724 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023725 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023726 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23727 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23728 {
23729 /* When there was "s:" already or the name expanded to get a
23730 * leading "s:" then remove it. */
23731 lv.ll_name += 2;
23732 len -= 2;
23733 lead = 2;
23734 }
23735 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023736 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023737 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023738 /* skip over "s:" and "g:" */
23739 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023740 lv.ll_name += 2;
23741 len = (int)(end - lv.ll_name);
23742 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023743
23744 /*
23745 * Copy the function name to allocated memory.
23746 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23747 * Accept <SNR>123_name() outside a script.
23748 */
23749 if (skip)
23750 lead = 0; /* do nothing */
23751 else if (lead > 0)
23752 {
23753 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023754 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23755 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023756 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023757 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023758 if (current_SID <= 0)
23759 {
23760 EMSG(_(e_usingsid));
23761 goto theend;
23762 }
23763 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23764 lead += (int)STRLEN(sid_buf);
23765 }
23766 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023767 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023768 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023769 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023770 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023771 goto theend;
23772 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023773 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023774 {
23775 char_u *cp = vim_strchr(lv.ll_name, ':');
23776
23777 if (cp != NULL && cp < end)
23778 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023779 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023780 goto theend;
23781 }
23782 }
23783
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023784 name = alloc((unsigned)(len + lead + 1));
23785 if (name != NULL)
23786 {
23787 if (lead > 0)
23788 {
23789 name[0] = K_SPECIAL;
23790 name[1] = KS_EXTRA;
23791 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023792 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023793 STRCPY(name + 3, sid_buf);
23794 }
23795 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023796 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023797 }
23798 *pp = end;
23799
23800theend:
23801 clear_lval(&lv);
23802 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803}
23804
23805/*
23806 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23807 * Return 2 if "p" starts with "s:".
23808 * Return 0 otherwise.
23809 */
23810 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023811eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023813 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23814 * the standard library function. */
23815 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23816 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 return 5;
23818 if (p[0] == 's' && p[1] == ':')
23819 return 2;
23820 return 0;
23821}
23822
23823/*
23824 * Return TRUE if "p" starts with "<SID>" or "s:".
23825 * Only works if eval_fname_script() returned non-zero for "p"!
23826 */
23827 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023828eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829{
23830 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23831}
23832
23833/*
23834 * List the head of the function: "name(arg1, arg2)".
23835 */
23836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023837list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838{
23839 int j;
23840
23841 msg_start();
23842 if (indent)
23843 MSG_PUTS(" ");
23844 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023845 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 {
23847 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023848 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 }
23850 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023851 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023853 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 {
23855 if (j)
23856 MSG_PUTS(", ");
23857 msg_puts(FUNCARG(fp, j));
23858 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023859 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 {
23861 if (j)
23862 MSG_PUTS(", ");
23863 MSG_PUTS("...");
23864 }
23865 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023866 if (fp->uf_flags & FC_ABORT)
23867 MSG_PUTS(" abort");
23868 if (fp->uf_flags & FC_RANGE)
23869 MSG_PUTS(" range");
23870 if (fp->uf_flags & FC_DICT)
23871 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023872 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023873 if (p_verbose > 0)
23874 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875}
23876
23877/*
23878 * Find a function by name, return pointer to it in ufuncs.
23879 * Return NULL for unknown function.
23880 */
23881 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023882find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023884 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023886 hi = hash_find(&func_hashtab, name);
23887 if (!HASHITEM_EMPTY(hi))
23888 return HI2UF(hi);
23889 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890}
23891
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023892#if defined(EXITFREE) || defined(PROTO)
23893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023894free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023895{
23896 hashitem_T *hi;
23897
23898 /* Need to start all over every time, because func_free() may change the
23899 * hash table. */
23900 while (func_hashtab.ht_used > 0)
23901 for (hi = func_hashtab.ht_array; ; ++hi)
23902 if (!HASHITEM_EMPTY(hi))
23903 {
23904 func_free(HI2UF(hi));
23905 break;
23906 }
23907}
23908#endif
23909
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023910 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023911translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023912{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023913 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023914 return find_internal_func(name) >= 0;
23915 return find_func(name) != NULL;
23916}
23917
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023918/*
23919 * Return TRUE if a function "name" exists.
23920 */
23921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023922function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023923{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023924 char_u *nm = name;
23925 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023926 int n = FALSE;
23927
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023928 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23929 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023930 nm = skipwhite(nm);
23931
23932 /* Only accept "funcname", "funcname ", "funcname (..." and
23933 * "funcname(...", not "funcname!...". */
23934 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023935 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023936 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023937 return n;
23938}
23939
Bram Moolenaara1544c02013-05-30 12:35:52 +020023940 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023941get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023942{
23943 char_u *nm = name;
23944 char_u *p;
23945
23946 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23947
23948 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023949 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023950 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023951
Bram Moolenaara1544c02013-05-30 12:35:52 +020023952 vim_free(p);
23953 return NULL;
23954}
23955
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023956/*
23957 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023958 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23959 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023960 */
23961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023962builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023963{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023964 char_u *p;
23965
23966 if (!ASCII_ISLOWER(name[0]))
23967 return FALSE;
23968 p = vim_strchr(name, AUTOLOAD_CHAR);
23969 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023970}
23971
Bram Moolenaar05159a02005-02-26 23:04:13 +000023972#if defined(FEAT_PROFILE) || defined(PROTO)
23973/*
23974 * Start profiling function "fp".
23975 */
23976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023977func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023978{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023979 int len = fp->uf_lines.ga_len;
23980
23981 if (len == 0)
23982 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023983 fp->uf_tm_count = 0;
23984 profile_zero(&fp->uf_tm_self);
23985 profile_zero(&fp->uf_tm_total);
23986 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023987 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023988 if (fp->uf_tml_total == NULL)
23989 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023990 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023991 if (fp->uf_tml_self == NULL)
23992 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023993 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023994 fp->uf_tml_idx = -1;
23995 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23996 || fp->uf_tml_self == NULL)
23997 return; /* out of memory */
23998
23999 fp->uf_profiling = TRUE;
24000}
24001
24002/*
24003 * Dump the profiling results for all functions in file "fd".
24004 */
24005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024006func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024007{
24008 hashitem_T *hi;
24009 int todo;
24010 ufunc_T *fp;
24011 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024012 ufunc_T **sorttab;
24013 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024014
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024015 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024016 if (todo == 0)
24017 return; /* nothing to dump */
24018
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024019 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024020
Bram Moolenaar05159a02005-02-26 23:04:13 +000024021 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24022 {
24023 if (!HASHITEM_EMPTY(hi))
24024 {
24025 --todo;
24026 fp = HI2UF(hi);
24027 if (fp->uf_profiling)
24028 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024029 if (sorttab != NULL)
24030 sorttab[st_len++] = fp;
24031
Bram Moolenaar05159a02005-02-26 23:04:13 +000024032 if (fp->uf_name[0] == K_SPECIAL)
24033 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24034 else
24035 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24036 if (fp->uf_tm_count == 1)
24037 fprintf(fd, "Called 1 time\n");
24038 else
24039 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24040 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24041 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24042 fprintf(fd, "\n");
24043 fprintf(fd, "count total (s) self (s)\n");
24044
24045 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24046 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024047 if (FUNCLINE(fp, i) == NULL)
24048 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024049 prof_func_line(fd, fp->uf_tml_count[i],
24050 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024051 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24052 }
24053 fprintf(fd, "\n");
24054 }
24055 }
24056 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024057
24058 if (sorttab != NULL && st_len > 0)
24059 {
24060 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24061 prof_total_cmp);
24062 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24063 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24064 prof_self_cmp);
24065 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24066 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024067
24068 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024069}
Bram Moolenaar73830342005-02-28 22:48:19 +000024070
24071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024072prof_sort_list(
24073 FILE *fd,
24074 ufunc_T **sorttab,
24075 int st_len,
24076 char *title,
24077 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024078{
24079 int i;
24080 ufunc_T *fp;
24081
24082 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24083 fprintf(fd, "count total (s) self (s) function\n");
24084 for (i = 0; i < 20 && i < st_len; ++i)
24085 {
24086 fp = sorttab[i];
24087 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24088 prefer_self);
24089 if (fp->uf_name[0] == K_SPECIAL)
24090 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24091 else
24092 fprintf(fd, " %s()\n", fp->uf_name);
24093 }
24094 fprintf(fd, "\n");
24095}
24096
24097/*
24098 * Print the count and times for one function or function line.
24099 */
24100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024101prof_func_line(
24102 FILE *fd,
24103 int count,
24104 proftime_T *total,
24105 proftime_T *self,
24106 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024107{
24108 if (count > 0)
24109 {
24110 fprintf(fd, "%5d ", count);
24111 if (prefer_self && profile_equal(total, self))
24112 fprintf(fd, " ");
24113 else
24114 fprintf(fd, "%s ", profile_msg(total));
24115 if (!prefer_self && profile_equal(total, self))
24116 fprintf(fd, " ");
24117 else
24118 fprintf(fd, "%s ", profile_msg(self));
24119 }
24120 else
24121 fprintf(fd, " ");
24122}
24123
24124/*
24125 * Compare function for total time sorting.
24126 */
24127 static int
24128#ifdef __BORLANDC__
24129_RTLENTRYF
24130#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024131prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024132{
24133 ufunc_T *p1, *p2;
24134
24135 p1 = *(ufunc_T **)s1;
24136 p2 = *(ufunc_T **)s2;
24137 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24138}
24139
24140/*
24141 * Compare function for self time sorting.
24142 */
24143 static int
24144#ifdef __BORLANDC__
24145_RTLENTRYF
24146#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024147prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024148{
24149 ufunc_T *p1, *p2;
24150
24151 p1 = *(ufunc_T **)s1;
24152 p2 = *(ufunc_T **)s2;
24153 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24154}
24155
Bram Moolenaar05159a02005-02-26 23:04:13 +000024156#endif
24157
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024158/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024159 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024160 * Return TRUE if a package was loaded.
24161 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024162 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024163script_autoload(
24164 char_u *name,
24165 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024166{
24167 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024168 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024169 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024170 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024171
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024172 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024173 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024174 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024175 return FALSE;
24176
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024177 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024178
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024179 /* Find the name in the list of previously loaded package names. Skip
24180 * "autoload/", it's always the same. */
24181 for (i = 0; i < ga_loaded.ga_len; ++i)
24182 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24183 break;
24184 if (!reload && i < ga_loaded.ga_len)
24185 ret = FALSE; /* was loaded already */
24186 else
24187 {
24188 /* Remember the name if it wasn't loaded already. */
24189 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24190 {
24191 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24192 tofree = NULL;
24193 }
24194
24195 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024196 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024197 ret = TRUE;
24198 }
24199
24200 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024201 return ret;
24202}
24203
24204/*
24205 * Return the autoload script name for a function or variable name.
24206 * Returns NULL when out of memory.
24207 */
24208 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024209autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024210{
24211 char_u *p;
24212 char_u *scriptname;
24213
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024214 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024215 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24216 if (scriptname == NULL)
24217 return FALSE;
24218 STRCPY(scriptname, "autoload/");
24219 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024220 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024221 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024222 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024223 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024224 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024225}
24226
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24228
24229/*
24230 * Function given to ExpandGeneric() to obtain the list of user defined
24231 * function names.
24232 */
24233 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024234get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024235{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024236 static long_u done;
24237 static hashitem_T *hi;
24238 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239
24240 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024242 done = 0;
24243 hi = func_hashtab.ht_array;
24244 }
24245 if (done < func_hashtab.ht_used)
24246 {
24247 if (done++ > 0)
24248 ++hi;
24249 while (HASHITEM_EMPTY(hi))
24250 ++hi;
24251 fp = HI2UF(hi);
24252
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024253 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024254 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024255
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024256 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24257 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258
24259 cat_func_name(IObuff, fp);
24260 if (xp->xp_context != EXPAND_USER_FUNC)
24261 {
24262 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024263 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 STRCAT(IObuff, ")");
24265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 return IObuff;
24267 }
24268 return NULL;
24269}
24270
24271#endif /* FEAT_CMDL_COMPL */
24272
24273/*
24274 * Copy the function name of "fp" to buffer "buf".
24275 * "buf" must be able to hold the function name plus three bytes.
24276 * Takes care of script-local function names.
24277 */
24278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024279cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024280{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024281 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 {
24283 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024284 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024285 }
24286 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024287 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288}
24289
24290/*
24291 * ":delfunction {name}"
24292 */
24293 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024294ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024296 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 char_u *p;
24298 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024299 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300
24301 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024302 name = trans_function_name(&p, eap->skip, 0, &fudi);
24303 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024305 {
24306 if (fudi.fd_dict != NULL && !eap->skip)
24307 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024310 if (!ends_excmd(*skipwhite(p)))
24311 {
24312 vim_free(name);
24313 EMSG(_(e_trailing));
24314 return;
24315 }
24316 eap->nextcmd = check_nextcmd(p);
24317 if (eap->nextcmd != NULL)
24318 *p = NUL;
24319
24320 if (!eap->skip)
24321 fp = find_func(name);
24322 vim_free(name);
24323
24324 if (!eap->skip)
24325 {
24326 if (fp == NULL)
24327 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024328 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329 return;
24330 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024331 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332 {
24333 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24334 return;
24335 }
24336
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024337 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024339 /* Delete the dict item that refers to the function, it will
24340 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024341 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024342 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024343 else
24344 func_free(fp);
24345 }
24346}
24347
24348/*
24349 * Free a function and remove it from the list of functions.
24350 */
24351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024352func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024353{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024354 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024355
24356 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024357 ga_clear_strings(&(fp->uf_args));
24358 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024359#ifdef FEAT_PROFILE
24360 vim_free(fp->uf_tml_count);
24361 vim_free(fp->uf_tml_total);
24362 vim_free(fp->uf_tml_self);
24363#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024364
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024365 /* remove the function from the function hashtable */
24366 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24367 if (HASHITEM_EMPTY(hi))
24368 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024369 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024370 hash_remove(&func_hashtab, hi);
24371
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024372 vim_free(fp);
24373}
24374
24375/*
24376 * Unreference a Function: decrement the reference count and free it when it
24377 * becomes zero. Only for numbered functions.
24378 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024380func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024381{
24382 ufunc_T *fp;
24383
24384 if (name != NULL && isdigit(*name))
24385 {
24386 fp = find_func(name);
24387 if (fp == NULL)
24388 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024389 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024390 {
24391 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024392 * when "uf_calls" becomes zero. */
24393 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024394 func_free(fp);
24395 }
24396 }
24397}
24398
24399/*
24400 * Count a reference to a Function.
24401 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024402 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024403func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024404{
24405 ufunc_T *fp;
24406
24407 if (name != NULL && isdigit(*name))
24408 {
24409 fp = find_func(name);
24410 if (fp == NULL)
24411 EMSG2(_(e_intern2), "func_ref()");
24412 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024413 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 }
24415}
24416
24417/*
24418 * Call a user function.
24419 */
24420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024421call_user_func(
24422 ufunc_T *fp, /* pointer to function */
24423 int argcount, /* nr of args */
24424 typval_T *argvars, /* arguments */
24425 typval_T *rettv, /* return value */
24426 linenr_T firstline, /* first line of range */
24427 linenr_T lastline, /* last line of range */
24428 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024429{
Bram Moolenaar33570922005-01-25 22:26:29 +000024430 char_u *save_sourcing_name;
24431 linenr_T save_sourcing_lnum;
24432 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024433 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024434 int save_did_emsg;
24435 static int depth = 0;
24436 dictitem_T *v;
24437 int fixvar_idx = 0; /* index in fixvar[] */
24438 int i;
24439 int ai;
24440 char_u numbuf[NUMBUFLEN];
24441 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024442 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443#ifdef FEAT_PROFILE
24444 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024445 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447
24448 /* If depth of calling is getting too high, don't execute the function */
24449 if (depth >= p_mfd)
24450 {
24451 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024452 rettv->v_type = VAR_NUMBER;
24453 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454 return;
24455 }
24456 ++depth;
24457
24458 line_breakcheck(); /* check for CTRL-C hit */
24459
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024460 fc = (funccall_T *)alloc(sizeof(funccall_T));
24461 fc->caller = current_funccal;
24462 current_funccal = fc;
24463 fc->func = fp;
24464 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024465 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024466 fc->linenr = 0;
24467 fc->returned = FALSE;
24468 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024469 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024470 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24471 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472
Bram Moolenaar33570922005-01-25 22:26:29 +000024473 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024474 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024475 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24476 * each argument variable and saves a lot of time.
24477 */
24478 /*
24479 * Init l: variables.
24480 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024481 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024482 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024483 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024484 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24485 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024486 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024487 name = v->di_key;
24488 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024489 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024490 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024491 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024492 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024493 v->di_tv.vval.v_dict = selfdict;
24494 ++selfdict->dv_refcount;
24495 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024496
Bram Moolenaar33570922005-01-25 22:26:29 +000024497 /*
24498 * Init a: variables.
24499 * Set a:0 to "argcount".
24500 * Set a:000 to a list with room for the "..." arguments.
24501 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024502 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024503 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024504 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024505 /* Use "name" to avoid a warning from some compiler that checks the
24506 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024507 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024508 name = v->di_key;
24509 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024510 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024511 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024512 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024513 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024514 v->di_tv.vval.v_list = &fc->l_varlist;
24515 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24516 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24517 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024518
24519 /*
24520 * Set a:firstline to "firstline" and a:lastline to "lastline".
24521 * Set a:name to named arguments.
24522 * Set a:N to the "..." arguments.
24523 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024524 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024525 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024526 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024527 (varnumber_T)lastline);
24528 for (i = 0; i < argcount; ++i)
24529 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024530 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024531 if (ai < 0)
24532 /* named argument a:name */
24533 name = FUNCARG(fp, i);
24534 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024535 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024536 /* "..." argument a:1, a:2, etc. */
24537 sprintf((char *)numbuf, "%d", ai + 1);
24538 name = numbuf;
24539 }
24540 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24541 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024542 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024543 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24544 }
24545 else
24546 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024547 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24548 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024549 if (v == NULL)
24550 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024551 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024552 }
24553 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024554 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024555
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024556 /* Note: the values are copied directly to avoid alloc/free.
24557 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024558 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024559 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024560
24561 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24562 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024563 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24564 fc->l_listitems[ai].li_tv = argvars[i];
24565 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024566 }
24567 }
24568
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569 /* Don't redraw while executing the function. */
24570 ++RedrawingDisabled;
24571 save_sourcing_name = sourcing_name;
24572 save_sourcing_lnum = sourcing_lnum;
24573 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024574 /* need space for function name + ("function " + 3) or "[number]" */
24575 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24576 + STRLEN(fp->uf_name) + 20;
24577 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 if (sourcing_name != NULL)
24579 {
24580 if (save_sourcing_name != NULL
24581 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024582 sprintf((char *)sourcing_name, "%s[%d]..",
24583 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 else
24585 STRCPY(sourcing_name, "function ");
24586 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24587
24588 if (p_verbose >= 12)
24589 {
24590 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024591 verbose_enter_scroll();
24592
Bram Moolenaar555b2802005-05-19 21:08:39 +000024593 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024594 if (p_verbose >= 14)
24595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024597 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024598 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024599 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600
24601 msg_puts((char_u *)"(");
24602 for (i = 0; i < argcount; ++i)
24603 {
24604 if (i > 0)
24605 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024606 if (argvars[i].v_type == VAR_NUMBER)
24607 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608 else
24609 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024610 /* Do not want errors such as E724 here. */
24611 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024612 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024613 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024614 if (s != NULL)
24615 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024616 if (vim_strsize(s) > MSG_BUF_CLEN)
24617 {
24618 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24619 s = buf;
24620 }
24621 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024622 vim_free(tofree);
24623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024624 }
24625 }
24626 msg_puts((char_u *)")");
24627 }
24628 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024629
24630 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631 --no_wait_return;
24632 }
24633 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024634#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024635 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024636 {
24637 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24638 func_do_profile(fp);
24639 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024640 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024641 {
24642 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024643 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024644 profile_zero(&fp->uf_tm_children);
24645 }
24646 script_prof_save(&wait_start);
24647 }
24648#endif
24649
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024651 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652 save_did_emsg = did_emsg;
24653 did_emsg = FALSE;
24654
24655 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024656 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24658
24659 --RedrawingDisabled;
24660
24661 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024662 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024664 clear_tv(rettv);
24665 rettv->v_type = VAR_NUMBER;
24666 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 }
24668
Bram Moolenaar05159a02005-02-26 23:04:13 +000024669#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024670 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024671 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024672 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024673 profile_end(&call_start);
24674 profile_sub_wait(&wait_start, &call_start);
24675 profile_add(&fp->uf_tm_total, &call_start);
24676 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024677 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024678 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024679 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24680 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024681 }
24682 }
24683#endif
24684
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685 /* when being verbose, mention the return value */
24686 if (p_verbose >= 12)
24687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024689 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024690
Bram Moolenaar071d4272004-06-13 20:20:40 +000024691 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024692 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024693 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024694 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024695 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024696 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024698 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024699 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024700 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024701 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024702
Bram Moolenaar555b2802005-05-19 21:08:39 +000024703 /* The value may be very long. Skip the middle part, so that we
24704 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024705 * truncate it at the end. Don't want errors such as E724 here. */
24706 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024707 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024708 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024709 if (s != NULL)
24710 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024711 if (vim_strsize(s) > MSG_BUF_CLEN)
24712 {
24713 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24714 s = buf;
24715 }
24716 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024717 vim_free(tofree);
24718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719 }
24720 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024721
24722 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723 --no_wait_return;
24724 }
24725
24726 vim_free(sourcing_name);
24727 sourcing_name = save_sourcing_name;
24728 sourcing_lnum = save_sourcing_lnum;
24729 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024730#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024731 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024732 script_prof_restore(&wait_start);
24733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024734
24735 if (p_verbose >= 12 && sourcing_name != NULL)
24736 {
24737 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024738 verbose_enter_scroll();
24739
Bram Moolenaar555b2802005-05-19 21:08:39 +000024740 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024742
24743 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024744 --no_wait_return;
24745 }
24746
24747 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024748 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024749 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024750
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024751 /* If the a:000 list and the l: and a: dicts are not referenced we can
24752 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024753 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24754 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24755 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24756 {
24757 free_funccal(fc, FALSE);
24758 }
24759 else
24760 {
24761 hashitem_T *hi;
24762 listitem_T *li;
24763 int todo;
24764
24765 /* "fc" is still in use. This can happen when returning "a:000" or
24766 * assigning "l:" to a global variable.
24767 * Link "fc" in the list for garbage collection later. */
24768 fc->caller = previous_funccal;
24769 previous_funccal = fc;
24770
24771 /* Make a copy of the a: variables, since we didn't do that above. */
24772 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24773 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24774 {
24775 if (!HASHITEM_EMPTY(hi))
24776 {
24777 --todo;
24778 v = HI2DI(hi);
24779 copy_tv(&v->di_tv, &v->di_tv);
24780 }
24781 }
24782
24783 /* Make a copy of the a:000 items, since we didn't do that above. */
24784 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24785 copy_tv(&li->li_tv, &li->li_tv);
24786 }
24787}
24788
24789/*
24790 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024791 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024792 */
24793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024794can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024795{
24796 return (fc->l_varlist.lv_copyID != copyID
24797 && fc->l_vars.dv_copyID != copyID
24798 && fc->l_avars.dv_copyID != copyID);
24799}
24800
24801/*
24802 * Free "fc" and what it contains.
24803 */
24804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024805free_funccal(
24806 funccall_T *fc,
24807 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024808{
24809 listitem_T *li;
24810
24811 /* The a: variables typevals may not have been allocated, only free the
24812 * allocated variables. */
24813 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24814
24815 /* free all l: variables */
24816 vars_clear(&fc->l_vars.dv_hashtab);
24817
24818 /* Free the a:000 variables if they were allocated. */
24819 if (free_val)
24820 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24821 clear_tv(&li->li_tv);
24822
24823 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824}
24825
24826/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024827 * Add a number variable "name" to dict "dp" with value "nr".
24828 */
24829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024830add_nr_var(
24831 dict_T *dp,
24832 dictitem_T *v,
24833 char *name,
24834 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024835{
24836 STRCPY(v->di_key, name);
24837 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24838 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24839 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024840 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024841 v->di_tv.vval.v_number = nr;
24842}
24843
24844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024845 * ":return [expr]"
24846 */
24847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024848ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849{
24850 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852 int returning = FALSE;
24853
24854 if (current_funccal == NULL)
24855 {
24856 EMSG(_("E133: :return not inside a function"));
24857 return;
24858 }
24859
24860 if (eap->skip)
24861 ++emsg_skip;
24862
24863 eap->nextcmd = NULL;
24864 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024865 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866 {
24867 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024868 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024870 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 }
24872 /* It's safer to return also on error. */
24873 else if (!eap->skip)
24874 {
24875 /*
24876 * Return unless the expression evaluation has been cancelled due to an
24877 * aborting error, an interrupt, or an exception.
24878 */
24879 if (!aborting())
24880 returning = do_return(eap, FALSE, TRUE, NULL);
24881 }
24882
24883 /* When skipping or the return gets pending, advance to the next command
24884 * in this line (!returning). Otherwise, ignore the rest of the line.
24885 * Following lines will be ignored by get_func_line(). */
24886 if (returning)
24887 eap->nextcmd = NULL;
24888 else if (eap->nextcmd == NULL) /* no argument */
24889 eap->nextcmd = check_nextcmd(arg);
24890
24891 if (eap->skip)
24892 --emsg_skip;
24893}
24894
24895/*
24896 * Return from a function. Possibly makes the return pending. Also called
24897 * for a pending return at the ":endtry" or after returning from an extra
24898 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024899 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024900 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901 * FALSE when the return gets pending.
24902 */
24903 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024904do_return(
24905 exarg_T *eap,
24906 int reanimate,
24907 int is_cmd,
24908 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909{
24910 int idx;
24911 struct condstack *cstack = eap->cstack;
24912
24913 if (reanimate)
24914 /* Undo the return. */
24915 current_funccal->returned = FALSE;
24916
24917 /*
24918 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24919 * not in its finally clause (which then is to be executed next) is found.
24920 * In this case, make the ":return" pending for execution at the ":endtry".
24921 * Otherwise, return normally.
24922 */
24923 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24924 if (idx >= 0)
24925 {
24926 cstack->cs_pending[idx] = CSTP_RETURN;
24927
24928 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024929 /* A pending return again gets pending. "rettv" points to an
24930 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024932 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933 else
24934 {
24935 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024936 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024938 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024940 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941 {
24942 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024943 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024944 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024945 else
24946 EMSG(_(e_outofmem));
24947 }
24948 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024949 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024950
24951 if (reanimate)
24952 {
24953 /* The pending return value could be overwritten by a ":return"
24954 * without argument in a finally clause; reset the default
24955 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024956 current_funccal->rettv->v_type = VAR_NUMBER;
24957 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024958 }
24959 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024960 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024961 }
24962 else
24963 {
24964 current_funccal->returned = TRUE;
24965
24966 /* If the return is carried out now, store the return value. For
24967 * a return immediately after reanimation, the value is already
24968 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024969 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024971 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024972 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024973 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024974 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975 }
24976 }
24977
24978 return idx < 0;
24979}
24980
24981/*
24982 * Free the variable with a pending return value.
24983 */
24984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024985discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024986{
Bram Moolenaar33570922005-01-25 22:26:29 +000024987 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988}
24989
24990/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024991 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992 * is an allocated string. Used by report_pending() for verbose messages.
24993 */
24994 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024995get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024997 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024998 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024999 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025001 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025002 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025003 if (s == NULL)
25004 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025005
25006 STRCPY(IObuff, ":return ");
25007 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25008 if (STRLEN(s) + 8 >= IOSIZE)
25009 STRCPY(IObuff + IOSIZE - 4, "...");
25010 vim_free(tofree);
25011 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012}
25013
25014/*
25015 * Get next function line.
25016 * Called by do_cmdline() to get the next line.
25017 * Returns allocated string, or NULL for end of function.
25018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025020get_func_line(
25021 int c UNUSED,
25022 void *cookie,
25023 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024{
Bram Moolenaar33570922005-01-25 22:26:29 +000025025 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025026 ufunc_T *fp = fcp->func;
25027 char_u *retval;
25028 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025029
25030 /* If breakpoints have been added/deleted need to check for it. */
25031 if (fcp->dbg_tick != debug_tick)
25032 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025033 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034 sourcing_lnum);
25035 fcp->dbg_tick = debug_tick;
25036 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025037#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025038 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025039 func_line_end(cookie);
25040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041
Bram Moolenaar05159a02005-02-26 23:04:13 +000025042 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025043 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25044 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025045 retval = NULL;
25046 else
25047 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025048 /* Skip NULL lines (continuation lines). */
25049 while (fcp->linenr < gap->ga_len
25050 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25051 ++fcp->linenr;
25052 if (fcp->linenr >= gap->ga_len)
25053 retval = NULL;
25054 else
25055 {
25056 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25057 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025058#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025059 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025060 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025061#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025063 }
25064
25065 /* Did we encounter a breakpoint? */
25066 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25067 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025068 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025069 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025070 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071 sourcing_lnum);
25072 fcp->dbg_tick = debug_tick;
25073 }
25074
25075 return retval;
25076}
25077
Bram Moolenaar05159a02005-02-26 23:04:13 +000025078#if defined(FEAT_PROFILE) || defined(PROTO)
25079/*
25080 * Called when starting to read a function line.
25081 * "sourcing_lnum" must be correct!
25082 * When skipping lines it may not actually be executed, but we won't find out
25083 * until later and we need to store the time now.
25084 */
25085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025086func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025087{
25088 funccall_T *fcp = (funccall_T *)cookie;
25089 ufunc_T *fp = fcp->func;
25090
25091 if (fp->uf_profiling && sourcing_lnum >= 1
25092 && sourcing_lnum <= fp->uf_lines.ga_len)
25093 {
25094 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025095 /* Skip continuation lines. */
25096 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25097 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025098 fp->uf_tml_execed = FALSE;
25099 profile_start(&fp->uf_tml_start);
25100 profile_zero(&fp->uf_tml_children);
25101 profile_get_wait(&fp->uf_tml_wait);
25102 }
25103}
25104
25105/*
25106 * Called when actually executing a function line.
25107 */
25108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025109func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025110{
25111 funccall_T *fcp = (funccall_T *)cookie;
25112 ufunc_T *fp = fcp->func;
25113
25114 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25115 fp->uf_tml_execed = TRUE;
25116}
25117
25118/*
25119 * Called when done with a function line.
25120 */
25121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025122func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025123{
25124 funccall_T *fcp = (funccall_T *)cookie;
25125 ufunc_T *fp = fcp->func;
25126
25127 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25128 {
25129 if (fp->uf_tml_execed)
25130 {
25131 ++fp->uf_tml_count[fp->uf_tml_idx];
25132 profile_end(&fp->uf_tml_start);
25133 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025134 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025135 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25136 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025137 }
25138 fp->uf_tml_idx = -1;
25139 }
25140}
25141#endif
25142
Bram Moolenaar071d4272004-06-13 20:20:40 +000025143/*
25144 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025145 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146 */
25147 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025148func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149{
Bram Moolenaar33570922005-01-25 22:26:29 +000025150 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151
25152 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25153 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025154 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155 || fcp->returned);
25156}
25157
25158/*
25159 * return TRUE if cookie indicates a function which "abort"s on errors.
25160 */
25161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025162func_has_abort(
25163 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025165 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166}
25167
25168#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25169typedef enum
25170{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025171 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25172 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25173 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025174} var_flavour_T;
25175
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025176static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177
25178 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025179var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180{
25181 char_u *p = varname;
25182
25183 if (ASCII_ISUPPER(*p))
25184 {
25185 while (*(++p))
25186 if (ASCII_ISLOWER(*p))
25187 return VAR_FLAVOUR_SESSION;
25188 return VAR_FLAVOUR_VIMINFO;
25189 }
25190 else
25191 return VAR_FLAVOUR_DEFAULT;
25192}
25193#endif
25194
25195#if defined(FEAT_VIMINFO) || defined(PROTO)
25196/*
25197 * Restore global vars that start with a capital from the viminfo file
25198 */
25199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025200read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201{
25202 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025203 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025204 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025205 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206
25207 if (!writing && (find_viminfo_parameter('!') != NULL))
25208 {
25209 tab = vim_strchr(virp->vir_line + 1, '\t');
25210 if (tab != NULL)
25211 {
25212 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025213 switch (*tab)
25214 {
25215 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025216#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025217 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025218#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025219 case 'D': type = VAR_DICT; break;
25220 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025221 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223
25224 tab = vim_strchr(tab, '\t');
25225 if (tab != NULL)
25226 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025227 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025228 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025229 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025231#ifdef FEAT_FLOAT
25232 else if (type == VAR_FLOAT)
25233 (void)string2float(tab + 1, &tv.vval.v_float);
25234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025236 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025237 if (type == VAR_DICT || type == VAR_LIST)
25238 {
25239 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25240
25241 if (etv == NULL)
25242 /* Failed to parse back the dict or list, use it as a
25243 * string. */
25244 tv.v_type = VAR_STRING;
25245 else
25246 {
25247 vim_free(tv.vval.v_string);
25248 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025249 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025250 }
25251 }
25252
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025253 /* when in a function use global variables */
25254 save_funccal = current_funccal;
25255 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025256 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025257 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025258
25259 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025260 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025261 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25262 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263 }
25264 }
25265 }
25266
25267 return viminfo_readline(virp);
25268}
25269
25270/*
25271 * Write global vars that start with a capital to the viminfo file
25272 */
25273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025274write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275{
Bram Moolenaar33570922005-01-25 22:26:29 +000025276 hashitem_T *hi;
25277 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025278 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025279 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025280 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025281 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025282 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283
25284 if (find_viminfo_parameter('!') == NULL)
25285 return;
25286
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025287 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025288
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025289 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025290 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025292 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025294 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025295 this_var = HI2DI(hi);
25296 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025297 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025298 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025299 {
25300 case VAR_STRING: s = "STR"; break;
25301 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025302 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025303 case VAR_DICT: s = "DIC"; break;
25304 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025305 case VAR_SPECIAL: s = "XPL"; break;
25306
25307 case VAR_UNKNOWN:
25308 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025309 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025310 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025311 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025312 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025313 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025314 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025315 if (p != NULL)
25316 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025317 vim_free(tofree);
25318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319 }
25320 }
25321}
25322#endif
25323
25324#if defined(FEAT_SESSION) || defined(PROTO)
25325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025326store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025327{
Bram Moolenaar33570922005-01-25 22:26:29 +000025328 hashitem_T *hi;
25329 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025330 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 char_u *p, *t;
25332
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025333 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025334 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025336 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025338 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025339 this_var = HI2DI(hi);
25340 if ((this_var->di_tv.v_type == VAR_NUMBER
25341 || this_var->di_tv.v_type == VAR_STRING)
25342 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025343 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025344 /* Escape special characters with a backslash. Turn a LF and
25345 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025346 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025347 (char_u *)"\\\"\n\r");
25348 if (p == NULL) /* out of memory */
25349 break;
25350 for (t = p; *t != NUL; ++t)
25351 if (*t == '\n')
25352 *t = 'n';
25353 else if (*t == '\r')
25354 *t = 'r';
25355 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025356 this_var->di_key,
25357 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25358 : ' ',
25359 p,
25360 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25361 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025362 || put_eol(fd) == FAIL)
25363 {
25364 vim_free(p);
25365 return FAIL;
25366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367 vim_free(p);
25368 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025369#ifdef FEAT_FLOAT
25370 else if (this_var->di_tv.v_type == VAR_FLOAT
25371 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25372 {
25373 float_T f = this_var->di_tv.vval.v_float;
25374 int sign = ' ';
25375
25376 if (f < 0)
25377 {
25378 f = -f;
25379 sign = '-';
25380 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025381 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025382 this_var->di_key, sign, f) < 0)
25383 || put_eol(fd) == FAIL)
25384 return FAIL;
25385 }
25386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387 }
25388 }
25389 return OK;
25390}
25391#endif
25392
Bram Moolenaar661b1822005-07-28 22:36:45 +000025393/*
25394 * Display script name where an item was last set.
25395 * Should only be invoked when 'verbose' is non-zero.
25396 */
25397 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025398last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025399{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025400 char_u *p;
25401
Bram Moolenaar661b1822005-07-28 22:36:45 +000025402 if (scriptID != 0)
25403 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025404 p = home_replace_save(NULL, get_scriptname(scriptID));
25405 if (p != NULL)
25406 {
25407 verbose_enter();
25408 MSG_PUTS(_("\n\tLast set from "));
25409 MSG_PUTS(p);
25410 vim_free(p);
25411 verbose_leave();
25412 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025413 }
25414}
25415
Bram Moolenaard812df62008-11-09 12:46:09 +000025416/*
25417 * List v:oldfiles in a nice way.
25418 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025420ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025421{
25422 list_T *l = vimvars[VV_OLDFILES].vv_list;
25423 listitem_T *li;
25424 int nr = 0;
25425
25426 if (l == NULL)
25427 msg((char_u *)_("No old files"));
25428 else
25429 {
25430 msg_start();
25431 msg_scroll = TRUE;
25432 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25433 {
25434 msg_outnum((long)++nr);
25435 MSG_PUTS(": ");
25436 msg_outtrans(get_tv_string(&li->li_tv));
25437 msg_putchar('\n');
25438 out_flush(); /* output one line at a time */
25439 ui_breakcheck();
25440 }
25441 /* Assume "got_int" was set to truncate the listing. */
25442 got_int = FALSE;
25443
25444#ifdef FEAT_BROWSE_CMD
25445 if (cmdmod.browse)
25446 {
25447 quit_more = FALSE;
25448 nr = prompt_for_number(FALSE);
25449 msg_starthere();
25450 if (nr > 0)
25451 {
25452 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25453 (long)nr);
25454
25455 if (p != NULL)
25456 {
25457 p = expand_env_save(p);
25458 eap->arg = p;
25459 eap->cmdidx = CMD_edit;
25460 cmdmod.browse = FALSE;
25461 do_exedit(eap, NULL);
25462 vim_free(p);
25463 }
25464 }
25465 }
25466#endif
25467 }
25468}
25469
Bram Moolenaar53744302015-07-17 17:38:22 +020025470/* reset v:option_new, v:option_old and v:option_type */
25471 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025472reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025473{
25474 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25475 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25476 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25477}
25478
25479
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480#endif /* FEAT_EVAL */
25481
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025483#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484
25485#ifdef WIN3264
25486/*
25487 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25488 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025489static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25490static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25491static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492
25493/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025494 * Get the short path (8.3) for the filename in "fnamep".
25495 * Only works for a valid file name.
25496 * When the path gets longer "fnamep" is changed and the allocated buffer
25497 * is put in "bufp".
25498 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25499 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500 */
25501 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025502get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025504 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505 char_u *newbuf;
25506
25507 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025508 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509 if (l > len - 1)
25510 {
25511 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025512 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 newbuf = vim_strnsave(*fnamep, l);
25514 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025515 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516
25517 vim_free(*bufp);
25518 *fnamep = *bufp = newbuf;
25519
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025520 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025521 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522 }
25523
25524 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025525 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025526}
25527
25528/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025529 * Get the short path (8.3) for the filename in "fname". The converted
25530 * path is returned in "bufp".
25531 *
25532 * Some of the directories specified in "fname" may not exist. This function
25533 * will shorten the existing directories at the beginning of the path and then
25534 * append the remaining non-existing path.
25535 *
25536 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025537 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025538 * bufp - Pointer to an allocated buffer for the filename.
25539 * fnamelen - Length of the filename pointed to by fname
25540 *
25541 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542 */
25543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025544shortpath_for_invalid_fname(
25545 char_u **fname,
25546 char_u **bufp,
25547 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025549 char_u *short_fname, *save_fname, *pbuf_unused;
25550 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025552 int old_len, len;
25553 int new_len, sfx_len;
25554 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025555
25556 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025557 old_len = *fnamelen;
25558 save_fname = vim_strnsave(*fname, old_len);
25559 pbuf_unused = NULL;
25560 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025562 endp = save_fname + old_len - 1; /* Find the end of the copy */
25563 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025564
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025565 /*
25566 * Try shortening the supplied path till it succeeds by removing one
25567 * directory at a time from the tail of the path.
25568 */
25569 len = 0;
25570 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025571 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025572 /* go back one path-separator */
25573 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25574 --endp;
25575 if (endp <= save_fname)
25576 break; /* processed the complete path */
25577
25578 /*
25579 * Replace the path separator with a NUL and try to shorten the
25580 * resulting path.
25581 */
25582 ch = *endp;
25583 *endp = 0;
25584 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025585 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025586 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25587 {
25588 retval = FAIL;
25589 goto theend;
25590 }
25591 *endp = ch; /* preserve the string */
25592
25593 if (len > 0)
25594 break; /* successfully shortened the path */
25595
25596 /* failed to shorten the path. Skip the path separator */
25597 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598 }
25599
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025600 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025601 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025602 /*
25603 * Succeeded in shortening the path. Now concatenate the shortened
25604 * path with the remaining path at the tail.
25605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025607 /* Compute the length of the new path. */
25608 sfx_len = (int)(save_endp - endp) + 1;
25609 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025610
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025611 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025613 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025615 /* There is not enough space in the currently allocated string,
25616 * copy it to a buffer big enough. */
25617 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025619 {
25620 retval = FAIL;
25621 goto theend;
25622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623 }
25624 else
25625 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025626 /* Transfer short_fname to the main buffer (it's big enough),
25627 * unless get_short_pathname() did its work in-place. */
25628 *fname = *bufp = save_fname;
25629 if (short_fname != save_fname)
25630 vim_strncpy(save_fname, short_fname, len);
25631 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025632 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025633
25634 /* concat the not-shortened part of the path */
25635 vim_strncpy(*fname + len, endp, sfx_len);
25636 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025637 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025638
25639theend:
25640 vim_free(pbuf_unused);
25641 vim_free(save_fname);
25642
25643 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025644}
25645
25646/*
25647 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025648 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025649 */
25650 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025651shortpath_for_partial(
25652 char_u **fnamep,
25653 char_u **bufp,
25654 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655{
25656 int sepcount, len, tflen;
25657 char_u *p;
25658 char_u *pbuf, *tfname;
25659 int hasTilde;
25660
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025661 /* Count up the path separators from the RHS.. so we know which part
25662 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025664 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025665 if (vim_ispathsep(*p))
25666 ++sepcount;
25667
25668 /* Need full path first (use expand_env() to remove a "~/") */
25669 hasTilde = (**fnamep == '~');
25670 if (hasTilde)
25671 pbuf = tfname = expand_env_save(*fnamep);
25672 else
25673 pbuf = tfname = FullName_save(*fnamep, FALSE);
25674
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025675 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025677 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25678 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025679
25680 if (len == 0)
25681 {
25682 /* Don't have a valid filename, so shorten the rest of the
25683 * path if we can. This CAN give us invalid 8.3 filenames, but
25684 * there's not a lot of point in guessing what it might be.
25685 */
25686 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025687 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25688 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689 }
25690
25691 /* Count the paths backward to find the beginning of the desired string. */
25692 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025693 {
25694#ifdef FEAT_MBYTE
25695 if (has_mbyte)
25696 p -= mb_head_off(tfname, p);
25697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025698 if (vim_ispathsep(*p))
25699 {
25700 if (sepcount == 0 || (hasTilde && sepcount == 1))
25701 break;
25702 else
25703 sepcount --;
25704 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025706 if (hasTilde)
25707 {
25708 --p;
25709 if (p >= tfname)
25710 *p = '~';
25711 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025712 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025713 }
25714 else
25715 ++p;
25716
25717 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25718 vim_free(*bufp);
25719 *fnamelen = (int)STRLEN(p);
25720 *bufp = pbuf;
25721 *fnamep = p;
25722
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025723 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025724}
25725#endif /* WIN3264 */
25726
25727/*
25728 * Adjust a filename, according to a string of modifiers.
25729 * *fnamep must be NUL terminated when called. When returning, the length is
25730 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025731 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025732 * When there is an error, *fnamep is set to NULL.
25733 */
25734 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025735modify_fname(
25736 char_u *src, /* string with modifiers */
25737 int *usedlen, /* characters after src that are used */
25738 char_u **fnamep, /* file name so far */
25739 char_u **bufp, /* buffer for allocated file name or NULL */
25740 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741{
25742 int valid = 0;
25743 char_u *tail;
25744 char_u *s, *p, *pbuf;
25745 char_u dirname[MAXPATHL];
25746 int c;
25747 int has_fullname = 0;
25748#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025749 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025750 int has_shortname = 0;
25751#endif
25752
25753repeat:
25754 /* ":p" - full path/file_name */
25755 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25756 {
25757 has_fullname = 1;
25758
25759 valid |= VALID_PATH;
25760 *usedlen += 2;
25761
25762 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25763 if ((*fnamep)[0] == '~'
25764#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25765 && ((*fnamep)[1] == '/'
25766# ifdef BACKSLASH_IN_FILENAME
25767 || (*fnamep)[1] == '\\'
25768# endif
25769 || (*fnamep)[1] == NUL)
25770
25771#endif
25772 )
25773 {
25774 *fnamep = expand_env_save(*fnamep);
25775 vim_free(*bufp); /* free any allocated file name */
25776 *bufp = *fnamep;
25777 if (*fnamep == NULL)
25778 return -1;
25779 }
25780
25781 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025782 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783 {
25784 if (vim_ispathsep(*p)
25785 && p[1] == '.'
25786 && (p[2] == NUL
25787 || vim_ispathsep(p[2])
25788 || (p[2] == '.'
25789 && (p[3] == NUL || vim_ispathsep(p[3])))))
25790 break;
25791 }
25792
25793 /* FullName_save() is slow, don't use it when not needed. */
25794 if (*p != NUL || !vim_isAbsName(*fnamep))
25795 {
25796 *fnamep = FullName_save(*fnamep, *p != NUL);
25797 vim_free(*bufp); /* free any allocated file name */
25798 *bufp = *fnamep;
25799 if (*fnamep == NULL)
25800 return -1;
25801 }
25802
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025803#ifdef WIN3264
25804# if _WIN32_WINNT >= 0x0500
25805 if (vim_strchr(*fnamep, '~') != NULL)
25806 {
25807 /* Expand 8.3 filename to full path. Needed to make sure the same
25808 * file does not have two different names.
25809 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25810 p = alloc(_MAX_PATH + 1);
25811 if (p != NULL)
25812 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025813 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025814 {
25815 vim_free(*bufp);
25816 *bufp = *fnamep = p;
25817 }
25818 else
25819 vim_free(p);
25820 }
25821 }
25822# endif
25823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025824 /* Append a path separator to a directory. */
25825 if (mch_isdir(*fnamep))
25826 {
25827 /* Make room for one or two extra characters. */
25828 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25829 vim_free(*bufp); /* free any allocated file name */
25830 *bufp = *fnamep;
25831 if (*fnamep == NULL)
25832 return -1;
25833 add_pathsep(*fnamep);
25834 }
25835 }
25836
25837 /* ":." - path relative to the current directory */
25838 /* ":~" - path relative to the home directory */
25839 /* ":8" - shortname path - postponed till after */
25840 while (src[*usedlen] == ':'
25841 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25842 {
25843 *usedlen += 2;
25844 if (c == '8')
25845 {
25846#ifdef WIN3264
25847 has_shortname = 1; /* Postpone this. */
25848#endif
25849 continue;
25850 }
25851 pbuf = NULL;
25852 /* Need full path first (use expand_env() to remove a "~/") */
25853 if (!has_fullname)
25854 {
25855 if (c == '.' && **fnamep == '~')
25856 p = pbuf = expand_env_save(*fnamep);
25857 else
25858 p = pbuf = FullName_save(*fnamep, FALSE);
25859 }
25860 else
25861 p = *fnamep;
25862
25863 has_fullname = 0;
25864
25865 if (p != NULL)
25866 {
25867 if (c == '.')
25868 {
25869 mch_dirname(dirname, MAXPATHL);
25870 s = shorten_fname(p, dirname);
25871 if (s != NULL)
25872 {
25873 *fnamep = s;
25874 if (pbuf != NULL)
25875 {
25876 vim_free(*bufp); /* free any allocated file name */
25877 *bufp = pbuf;
25878 pbuf = NULL;
25879 }
25880 }
25881 }
25882 else
25883 {
25884 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25885 /* Only replace it when it starts with '~' */
25886 if (*dirname == '~')
25887 {
25888 s = vim_strsave(dirname);
25889 if (s != NULL)
25890 {
25891 *fnamep = s;
25892 vim_free(*bufp);
25893 *bufp = s;
25894 }
25895 }
25896 }
25897 vim_free(pbuf);
25898 }
25899 }
25900
25901 tail = gettail(*fnamep);
25902 *fnamelen = (int)STRLEN(*fnamep);
25903
25904 /* ":h" - head, remove "/file_name", can be repeated */
25905 /* Don't remove the first "/" or "c:\" */
25906 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25907 {
25908 valid |= VALID_HEAD;
25909 *usedlen += 2;
25910 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025911 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025912 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 *fnamelen = (int)(tail - *fnamep);
25914#ifdef VMS
25915 if (*fnamelen > 0)
25916 *fnamelen += 1; /* the path separator is part of the path */
25917#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025918 if (*fnamelen == 0)
25919 {
25920 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25921 p = vim_strsave((char_u *)".");
25922 if (p == NULL)
25923 return -1;
25924 vim_free(*bufp);
25925 *bufp = *fnamep = tail = p;
25926 *fnamelen = 1;
25927 }
25928 else
25929 {
25930 while (tail > s && !after_pathsep(s, tail))
25931 mb_ptr_back(*fnamep, tail);
25932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025933 }
25934
25935 /* ":8" - shortname */
25936 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25937 {
25938 *usedlen += 2;
25939#ifdef WIN3264
25940 has_shortname = 1;
25941#endif
25942 }
25943
25944#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025945 /*
25946 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947 */
25948 if (has_shortname)
25949 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025950 /* Copy the string if it is shortened by :h and when it wasn't copied
25951 * yet, because we are going to change it in place. Avoids changing
25952 * the buffer name for "%:8". */
25953 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954 {
25955 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025956 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025957 return -1;
25958 vim_free(*bufp);
25959 *bufp = *fnamep = p;
25960 }
25961
25962 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025963 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025964 if (!has_fullname && !vim_isAbsName(*fnamep))
25965 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025966 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025967 return -1;
25968 }
25969 else
25970 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025971 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025972
Bram Moolenaardc935552011-08-17 15:23:23 +020025973 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025974 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025975 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025976 return -1;
25977
25978 if (l == 0)
25979 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025980 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025981 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025982 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025983 return -1;
25984 }
25985 *fnamelen = l;
25986 }
25987 }
25988#endif /* WIN3264 */
25989
25990 /* ":t" - tail, just the basename */
25991 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25992 {
25993 *usedlen += 2;
25994 *fnamelen -= (int)(tail - *fnamep);
25995 *fnamep = tail;
25996 }
25997
25998 /* ":e" - extension, can be repeated */
25999 /* ":r" - root, without extension, can be repeated */
26000 while (src[*usedlen] == ':'
26001 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26002 {
26003 /* find a '.' in the tail:
26004 * - for second :e: before the current fname
26005 * - otherwise: The last '.'
26006 */
26007 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26008 s = *fnamep - 2;
26009 else
26010 s = *fnamep + *fnamelen - 1;
26011 for ( ; s > tail; --s)
26012 if (s[0] == '.')
26013 break;
26014 if (src[*usedlen + 1] == 'e') /* :e */
26015 {
26016 if (s > tail)
26017 {
26018 *fnamelen += (int)(*fnamep - (s + 1));
26019 *fnamep = s + 1;
26020#ifdef VMS
26021 /* cut version from the extension */
26022 s = *fnamep + *fnamelen - 1;
26023 for ( ; s > *fnamep; --s)
26024 if (s[0] == ';')
26025 break;
26026 if (s > *fnamep)
26027 *fnamelen = s - *fnamep;
26028#endif
26029 }
26030 else if (*fnamep <= tail)
26031 *fnamelen = 0;
26032 }
26033 else /* :r */
26034 {
26035 if (s > tail) /* remove one extension */
26036 *fnamelen = (int)(s - *fnamep);
26037 }
26038 *usedlen += 2;
26039 }
26040
26041 /* ":s?pat?foo?" - substitute */
26042 /* ":gs?pat?foo?" - global substitute */
26043 if (src[*usedlen] == ':'
26044 && (src[*usedlen + 1] == 's'
26045 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26046 {
26047 char_u *str;
26048 char_u *pat;
26049 char_u *sub;
26050 int sep;
26051 char_u *flags;
26052 int didit = FALSE;
26053
26054 flags = (char_u *)"";
26055 s = src + *usedlen + 2;
26056 if (src[*usedlen + 1] == 'g')
26057 {
26058 flags = (char_u *)"g";
26059 ++s;
26060 }
26061
26062 sep = *s++;
26063 if (sep)
26064 {
26065 /* find end of pattern */
26066 p = vim_strchr(s, sep);
26067 if (p != NULL)
26068 {
26069 pat = vim_strnsave(s, (int)(p - s));
26070 if (pat != NULL)
26071 {
26072 s = p + 1;
26073 /* find end of substitution */
26074 p = vim_strchr(s, sep);
26075 if (p != NULL)
26076 {
26077 sub = vim_strnsave(s, (int)(p - s));
26078 str = vim_strnsave(*fnamep, *fnamelen);
26079 if (sub != NULL && str != NULL)
26080 {
26081 *usedlen = (int)(p + 1 - src);
26082 s = do_string_sub(str, pat, sub, flags);
26083 if (s != NULL)
26084 {
26085 *fnamep = s;
26086 *fnamelen = (int)STRLEN(s);
26087 vim_free(*bufp);
26088 *bufp = s;
26089 didit = TRUE;
26090 }
26091 }
26092 vim_free(sub);
26093 vim_free(str);
26094 }
26095 vim_free(pat);
26096 }
26097 }
26098 /* after using ":s", repeat all the modifiers */
26099 if (didit)
26100 goto repeat;
26101 }
26102 }
26103
Bram Moolenaar26df0922014-02-23 23:39:13 +010026104 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26105 {
26106 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26107 if (p == NULL)
26108 return -1;
26109 vim_free(*bufp);
26110 *bufp = *fnamep = p;
26111 *fnamelen = (int)STRLEN(p);
26112 *usedlen += 2;
26113 }
26114
Bram Moolenaar071d4272004-06-13 20:20:40 +000026115 return valid;
26116}
26117
26118/*
26119 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26120 * "flags" can be "g" to do a global substitute.
26121 * Returns an allocated string, NULL for error.
26122 */
26123 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026124do_string_sub(
26125 char_u *str,
26126 char_u *pat,
26127 char_u *sub,
26128 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026129{
26130 int sublen;
26131 regmatch_T regmatch;
26132 int i;
26133 int do_all;
26134 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026135 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026136 garray_T ga;
26137 char_u *ret;
26138 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026139 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026140
26141 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26142 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026143 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144
26145 ga_init2(&ga, 1, 200);
26146
26147 do_all = (flags[0] == 'g');
26148
26149 regmatch.rm_ic = p_ic;
26150 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26151 if (regmatch.regprog != NULL)
26152 {
26153 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026154 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026155 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26156 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026157 /* Skip empty match except for first match. */
26158 if (regmatch.startp[0] == regmatch.endp[0])
26159 {
26160 if (zero_width == regmatch.startp[0])
26161 {
26162 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026163 i = MB_PTR2LEN(tail);
26164 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26165 (size_t)i);
26166 ga.ga_len += i;
26167 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026168 continue;
26169 }
26170 zero_width = regmatch.startp[0];
26171 }
26172
Bram Moolenaar071d4272004-06-13 20:20:40 +000026173 /*
26174 * Get some space for a temporary buffer to do the substitution
26175 * into. It will contain:
26176 * - The text up to where the match is.
26177 * - The substituted text.
26178 * - The text after the match.
26179 */
26180 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026181 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026182 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26183 {
26184 ga_clear(&ga);
26185 break;
26186 }
26187
26188 /* copy the text up to where the match is */
26189 i = (int)(regmatch.startp[0] - tail);
26190 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26191 /* add the substituted text */
26192 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26193 + ga.ga_len + i, TRUE, TRUE, FALSE);
26194 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026195 tail = regmatch.endp[0];
26196 if (*tail == NUL)
26197 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198 if (!do_all)
26199 break;
26200 }
26201
26202 if (ga.ga_data != NULL)
26203 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26204
Bram Moolenaar473de612013-06-08 18:19:48 +020026205 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026206 }
26207
26208 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26209 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026210 if (p_cpo == empty_option)
26211 p_cpo = save_cpo;
26212 else
26213 /* Darn, evaluating {sub} expression changed the value. */
26214 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026215
26216 return ret;
26217}
26218
26219#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */